<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"html/template"
	"net/http"
	"net/url"
	"strings"

	"willnorris.com/go/microformats"
)

var addr = flag.String("addr", ":4001", "Address and port to listen on")

func main() {
	flag.Parse()

	http.Handle("/", http.HandlerFunc(index))
	http.ListenAndServe(*addr, nil)
}

func index(w http.ResponseWriter, r *http.Request) {
	var parsedURL *url.URL
	var err error

	u := r.FormValue("url")
	if u != "" {
		parsedURL, err = url.Parse(u)
		if err != nil {
			http.Error(w, fmt.Sprintf("error parsing url: %v", err), http.StatusBadRequest)
		}
	}

	if r.Method == "GET" && parsedURL != nil {
		resp, err := http.Get(parsedURL.String())
		if err != nil {
			http.Error(w, fmt.Sprintf("error fetching url content: %v", err), http.StatusInternalServerError)
		}
		defer resp.Body.Close()

		mf := microformats.Parse(resp.Body, parsedURL)
		j, err := json.MarshalIndent(mf, "", "    ")
		if err != nil {
			http.Error(w, fmt.Sprintf("error marshaling json: %v", err), http.StatusInternalServerError)
		}

		w.Write(j)
		return
	}

	html := r.FormValue("html")
	var j []byte
	if html != "" {
		mf := microformats.Parse(strings.NewReader(html), parsedURL)
		j, err = json.MarshalIndent(mf, "", "    ")
		if err != nil {
			http.Error(w, fmt.Sprintf("error marshaling json: %v", err), http.StatusInternalServerError)
		}
	}

	data := struct {
		HTML string
		URL  string
		JSON string
	}{
		html,
		u,
		string(j),
	}

	tpl.Execute(w, data)
}

var tpl = template.Must(template.New("").Parse(`<!doctype html>

Parse a URL

  <form method="GET">
    <input name="url" type="url" />
    <input type="submit" value="Parse" />
  </form>

Parse HTML

  <form method="POST">
    <textarea name="html" style="width: 100%;" rows="15">{{ .HTML }}</textarea>

    <input name="url" type="text" style="width: 100%;" value="{{ .URL }}"></input>

    <input type="submit" value="Parse"/>
  </form>

{{ .JSON }}

`))
<commit_msg>gomfweb: add jsonp support and some docs<commit_after>// The gomfweb command runs a simple web server that demonstrates the use of
// the go microformats library.  It can parse the microformats found at a URL
// or in a provided snippet of HTML.
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"html/template"
	"net/http"
	"net/url"
	"strings"

	"willnorris.com/go/microformats"
)

var addr = flag.String("addr", ":4001", "Address and port to listen on")

func main() {
	flag.Parse()

	http.Handle("/", http.HandlerFunc(index))

	fmt.Printf("gomfweb listening on %s\n", *addr)
	http.ListenAndServe(*addr, nil)
}

func index(w http.ResponseWriter, r *http.Request) {
	var parsedURL *url.URL
	var err error

	u := r.FormValue("url")
	if u != "" {
		parsedURL, err = url.Parse(u)
		if err != nil {
			http.Error(w, fmt.Sprintf("error parsing url: %v", err), http.StatusBadRequest)
		}
	}

	if r.Method == "GET" && parsedURL != nil {
		resp, err := http.Get(parsedURL.String())
		if err != nil {
			http.Error(w, fmt.Sprintf("error fetching url content: %v", err), http.StatusInternalServerError)
		}
		defer resp.Body.Close()

		mf := microformats.Parse(resp.Body, parsedURL)
		j, err := json.MarshalIndent(mf, "", "    ")
		if err != nil {
			http.Error(w, fmt.Sprintf("error marshaling json: %v", err), http.StatusInternalServerError)
		}

		if callback := r.FormValue("callback"); callback != "" {
			fmt.Fprintf(w, "%s(%s)", callback, j)
		} else {
			w.Write(j)
		}
		return
	}

	html := r.FormValue("html")
	var j []byte
	if html != "" {
		mf := microformats.Parse(strings.NewReader(html), parsedURL)
		j, err = json.MarshalIndent(mf, "", "    ")
		if err != nil {
			http.Error(w, fmt.Sprintf("error marshaling json: %v", err), http.StatusInternalServerError)
		}
	}

	data := struct {
		HTML string
		URL  string
		JSON string
	}{
		html,
		u,
		string(j),
	}

	tpl.Execute(w, data)
}

var tpl = template.Must(template.New("").Parse(`<!doctype html>

Parse a URL

  <form method="GET">
    <input name="url" type="url" />
    <input type="submit" value="Parse" />
  </form>

Parse HTML

  <form method="POST">
    <textarea name="html" style="width: 100%;" rows="15">{{ .HTML }}</textarea>

    <input name="url" type="text" style="width: 100%;" value="{{ .URL }}"></input>

    <input type="submit" value="Parse"/>
  </form>

{{ .JSON }}

`))
<|endoftext|>


<commit_before>// Hap - the simple and effective provisioner
// Copyright (c) 2015 Garrett Woodworth (https://github.com/gwoo)
// The BSD License http://opensource.org/licenses/bsd-license.php.

package cli

import (
	"fmt"

	"github.com/gwoo/hap"
)

// Add the Push command
func init() {
	Commands.Add("push", &PushCmd{})
}

// Push command
type PushCmd struct{}

// Does this command expect a remote
func (cmd *PushCmd) IsRemote() bool {
	return true
}

// Get help on the push command
func (cmd *PushCmd) Help() string {
	return "hap push\tPush current repo to the remote."
}

// Push to the remote
func (cmd *PushCmd) Run(remote *hap.Remote) (string, error) {
	if err := remote.PushSubmodules(); err != nil {
		result := fmt.Sprintf("[%s] push failed.", remote.Host.Name)
		return result, err
	}
	if err := remote.Push(); err != nil {
		result := fmt.Sprintf("[%s] push failed.", remote.Host.Name)
		return result, err
	}
	result := fmt.Sprintf("[%s] push completed.", remote.Host.Name)
	return result, nil
}
<commit_msg>Fix warnings from go lint in cmd/hap/cli/push.go<commit_after>// Hap - the simple and effective provisioner
// Copyright (c) 2015 Garrett Woodworth (https://github.com/gwoo)
// The BSD License http://opensource.org/licenses/bsd-license.php.

package cli

import (
	"fmt"

	"github.com/gwoo/hap"
)

// Add the Push command
func init() {
	Commands.Add("push", &PushCmd{})
}

// PushCmd is the push command
type PushCmd struct{}

// IsRemote returns whether the command expects a remote
func (cmd *PushCmd) IsRemote() bool {
	return true
}

// Help returns help on the hap push command
func (cmd *PushCmd) Help() string {
	return "hap push\tPush current repo to the remote."
}

// Run takes a remote and pushes to it
func (cmd *PushCmd) Run(remote *hap.Remote) (string, error) {
	if err := remote.PushSubmodules(); err != nil {
		result := fmt.Sprintf("[%s] push failed.", remote.Host.Name)
		return result, err
	}
	if err := remote.Push(); err != nil {
		result := fmt.Sprintf("[%s] push failed.", remote.Host.Name)
		return result, err
	}
	result := fmt.Sprintf("[%s] push completed.", remote.Host.Name)
	return result, nil
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/spf13/cobra"
)

// Debug Command
var debugCmd = &cobra.Command{
	Use:   "debug",
	Short: "commands for debugging InfluxDB",
}

func debugF(cmd *cobra.Command, args []string) {
	cmd.Usage()
}

func init() {
	debugCmd.AddCommand(initInspectReportTSMCommand()) // Add report-tsm command
}
<commit_msg>Static analysis<commit_after>package main

import (
	"github.com/spf13/cobra"
)

// Debug Command
var debugCmd = &cobra.Command{
	Use:   "debug",
	Short: "commands for debugging InfluxDB",
}

func init() {
	debugCmd.AddCommand(initInspectReportTSMCommand()) // Add report-tsm command
}
<|endoftext|>


<commit_before>package list

import (
	"context"
	"errors"
	"fmt"
	"log"

	"github.com/danielkrainas/gobag/cmd"
	"github.com/danielkrainas/gobag/context"

	"github.com/danielkrainas/shex/manager"
)

func init() {
	cmd.Register("list", Info)
}

func listWrapper(fn func(*manager.ExecutionContext, []string) error) func(context.Context, []string) error {
	return func(ctx context.Context, args []string) error {
		ctx, err := manager.Context(ctx, "")
		if err != nil {
			return err
		}

		return fn(ctx, args)
	}
}

var (
	Info = &cmd.Info{
		Use:   "list",
		Short: "list",
		Long:  "list",
		Run:   cmd.ExecutorFunc(run),
		Commands: []*cmd.Info{
			{
				Use:   "mods",
				Short: "mods",
				Long:  "mods",
				Run:   cmd.ExecutorFunc(listWrapper(listMods)),
			},

			{
				Use:   "games",
				Short: "games",
				Long:  "games",
				Run:   cmd.ExecutorFunc(listWrapper(listGames)),
			},
			{
				Use:   "profiles",
				Short: "profiles",
				Long:  "profiles",
				Run:   cmd.ExecutorFunc(listWrapper(listProfiles)),
			},
			{
				Use:   "config",
				Short: "config",
				Long:  "config",
				Run:   cmd.ExecutorFunc(listWrapper(listConfig)),
			},
			{
				Use:   "channels",
				Short: "channels",
				Long:  "channels",
				Run:   cmd.ExecutorFunc(listWrapper(listChannels)),
			},
		},
	}
)

/* List Mods Command
type listModsCommand struct {
	Profile string `short:"p" long:"profile" description:"display mods installed in a profile"`
} */

func listMods(ctx *manager.ExecutionContext, args []string) error {
	profileName := acontext.GetStringValue(ctx, "flags.profile")
	useProfile := profileName != ""
	var mods ModList
	if useProfile {
		profileName = cmd.Profile
		if len(cmd.Profile) > 0 {
			selectedProfile, ok := ctx.Profiles[profileName]
			if !ok {
				return fmt.Errorf("profile not found: %q", profileName)
			}

			mods = selectedProfile.Mods
		} else {
			profileName = ctx.Profile.Name
			mods = ctx.Profile.Mods
		}
	} else if len(ctx.Config.Games) <= 0 {
		log.Println("no games attached")
		return nil
	} else {
		gameName := ""
		if len(args) > 0 {
			gameName = args[0]
		}

		gamePath := getGameOrDefault(ctx.Config.Games, gameName)
		manifest, err := loadGameManifest(gamePath)
		if err != nil {
			log.Errorf("error loading game manifest: %v", err)
			log.Println("game manifest not found or invalid")
			return nil
		}

		mods = manifest.Mods
	}

	//fmt.Printf("%-30s   %s\n", "NAME", "VERSION")
	if len(mods) > 0 {
		if useProfile {
			log.Printf("Mods installed in profile %s\n", profileName)
		}

		for name, version := range mods {
			log.Printf("%15s@%s\n", name, version)
		}
	} else {
		log.Printf("no mods installed\n")
	}

	return nil
}

/* List Config Command */
func listConfig(ctx *manager.ExecutionContext, _ []string) error {
	log.Printf("Settings: \n")
	log.Printf("    profile=%s\n", ctx.Config.ActiveProfile)
	log.Printf("    channel=%s\n", ctx.Config.ActiveRemote)
	return nil
}

/* List Games Command */
func listGames(ctx *manager.ExecutionContext, _ []string) error {
	if len(ctx.Config.Games) <= 0 {
		log.Printf("no games found.\n")
		return nil
	}

	log.Printf("%12s   %s\n", "ALIAS", "FOLDER")
	for alias, gameFolder := range ctx.Config.Games {
		log.Printf("%12s   %s\n", alias, gameFolder)
	}

	return nil
}

/* List Profiles Command */
func listProfiles(ctx *manager.ExecutionContext, _ []string) error {
	log.Printf("%15s   %s\n", "ID", "NAME")
	for _, p := range ctx.Profiles {
		log.Printf("%15s   %s\n", p.Id, p.Name)
	}

	return nil
}

/* List Channels Command */
func listChannels(ctx *manager.ExecutionContext, _ []string) error {
	format := "%15s  %10s   %s\n"
	log.Printf(format, "alias", "protocol", "endpoint")
	log.Printf(format, "==========", "========", "==========")
	for _, ch := range ctx.Channels {
		log.Printf(format, ch.Alias, ch.Protocol, ch.Endpoint)
	}

	return nil
}
<commit_msg>cleanup list command<commit_after>package list

import (
	"context"
	"fmt"
	"log"

	"github.com/danielkrainas/gobag/cmd"
	"github.com/danielkrainas/gobag/context"

	"github.com/danielkrainas/shex/api/v1"
	"github.com/danielkrainas/shex/manager"
	"github.com/danielkrainas/shex/mods"
)

func init() {
	cmd.Register("list", Info)
}

func listWrapper(fn func(*manager.ExecutionContext, []string) error) func(context.Context, []string) error {
	return func(parent context.Context, args []string) error {
		ctx, err := manager.Context(parent, "")
		if err != nil {
			return err
		}

		return fn(ctx, args)
	}
}

var (
	Info = &cmd.Info{
		Use:   "list",
		Short: "list",
		Long:  "list",
		SubCommands: []*cmd.Info{
			{
				Use:   "mods",
				Short: "mods",
				Long:  "mods",
				Run:   cmd.ExecutorFunc(listWrapper(listMods)),
			},

			{
				Use:   "games",
				Short: "games",
				Long:  "games",
				Run:   cmd.ExecutorFunc(listWrapper(listGames)),
			},
			{
				Use:   "profiles",
				Short: "profiles",
				Long:  "profiles",
				Run:   cmd.ExecutorFunc(listWrapper(listProfiles)),
			},
			{
				Use:   "config",
				Short: "config",
				Long:  "config",
				Run:   cmd.ExecutorFunc(listWrapper(listConfig)),
			},
			{
				Use:   "channels",
				Short: "channels",
				Long:  "channels",
				Run:   cmd.ExecutorFunc(listWrapper(listChannels)),
			},
		},
	}
)

/* List Mods Command
type listModsCommand struct {
	Profile string `short:"p" long:"profile" description:"display mods installed in a profile"`
} */

func listMods(ctx *manager.ExecutionContext, args []string) error {
	profileName := acontext.GetStringValue(ctx, "flags.profile")
	useProfile := profileName != ""
	var list v1.ModList
	if useProfile {
		profileName = ctx.Value("flags.profile").(string)
		if len(profileName) > 0 {
			selectedProfile, ok := ctx.Profiles[profileName]
			if !ok {
				return fmt.Errorf("profile not found: %q", profileName)
			}

			list = selectedProfile.Mods
		} else {
			profileName = ctx.Profile().Name
			list = ctx.Profile().Mods
		}
	} else if len(ctx.Config.Games) <= 0 {
		log.Println("no games attached")
		return nil
	} else {
		gameName := ""
		if len(args) > 0 {
			gameName = args[0]
		}

		gamePath := manager.GetGameOrDefault(ctx.Config.Games, gameName)
		manifest, err := mods.LoadGameManifest(gamePath)
		if err != nil {
			log.Printf("error loading game manifest: %v", err)
			log.Println("game manifest not found or invalid")
			return nil
		}

		list = manifest.Mods
	}

	//fmt.Printf("%-30s   %s\n", "NAME", "VERSION")
	if len(list) > 0 {
		if useProfile {
			log.Printf("Mods installed in profile %s\n", profileName)
		}

		for name, version := range list {
			log.Printf("%15s@%s\n", name, version)
		}
	} else {
		log.Printf("no mods installed\n")
	}

	return nil
}

/* List Config Command */
func listConfig(ctx *manager.ExecutionContext, _ []string) error {
	log.Printf("Settings: \n")
	log.Printf("    profile=%s\n", ctx.Config.ActiveProfile)
	log.Printf("    channel=%s\n", ctx.Config.ActiveRemote)
	return nil
}

/* List Games Command */
func listGames(ctx *manager.ExecutionContext, _ []string) error {
	if len(ctx.Config.Games) <= 0 {
		log.Printf("no games found.\n")
		return nil
	}

	log.Printf("%12s   %s\n", "ALIAS", "FOLDER")
	for alias, gameFolder := range ctx.Config.Games {
		log.Printf("%12s   %s\n", alias, gameFolder)
	}

	return nil
}

/* List Profiles Command */
func listProfiles(ctx *manager.ExecutionContext, _ []string) error {
	log.Printf("%15s   %s\n", "ID", "NAME")
	for _, p := range ctx.Profiles {
		log.Printf("%15s   %s\n", p.Id, p.Name)
	}

	return nil
}

/* List Channels Command */
func listChannels(ctx *manager.ExecutionContext, _ []string) error {
	format := "%15s  %10s   %s\n"
	log.Printf(format, "alias", "protocol", "endpoint")
	log.Printf(format, "==========", "========", "==========")
	for _, ch := range ctx.Channels {
		log.Printf(format, ch.Alias, ch.Protocol, ch.Endpoint)
	}

	return nil
}
<|endoftext|>


<commit_before>/*

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

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

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

package main

import (
	"flag"
	"os"
	"time"

	opa "github.com/open-policy-agent/frameworks/constraint/pkg/client"
	"github.com/open-policy-agent/frameworks/constraint/pkg/client/drivers/local"
	"github.com/open-policy-agent/gatekeeper/pkg/apis"
	"github.com/open-policy-agent/gatekeeper/pkg/audit"
	"github.com/open-policy-agent/gatekeeper/pkg/controller"
	"github.com/open-policy-agent/gatekeeper/pkg/target"
	"github.com/open-policy-agent/gatekeeper/pkg/upgrade"
	"github.com/open-policy-agent/gatekeeper/pkg/webhook"
	_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
	"sigs.k8s.io/controller-runtime/pkg/client/config"
	"sigs.k8s.io/controller-runtime/pkg/manager"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
	"sigs.k8s.io/controller-runtime/pkg/runtime/signals"

	"github.com/go-logr/zapr"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var (
	logLevel = flag.String("log-level", "INFO", "Minimum log level. For example, DEBUG, INFO, WARNING, ERROR. Defaulted to INFO if unspecified.")
)

func main() {

	flag.Parse()
	switch *logLevel {
	case "DEBUG":
		logf.SetLogger(logf.ZapLogger(true))
	case "WARNING", "ERROR":
		setLoggerForProduction()
	case "INFO":
		fallthrough
	default:
		logf.SetLogger(logf.ZapLogger(false))
	}

	log := logf.Log.WithName("entrypoint")

	// Get a config to talk to the apiserver
	log.Info("setting up client for manager")
	cfg, err := config.GetConfig()
	if err != nil {
		log.Error(err, "unable to set up client config")
		os.Exit(1)
	}

	// Create a new Cmd to provide shared dependencies and start components
	log.Info("setting up manager")
	mgr, err := manager.New(cfg, manager.Options{})
	if err != nil {
		log.Error(err, "unable to set up overall controller manager")
		os.Exit(1)
	}

	log.Info("Registering Components.")

	// Setup Scheme for all resources
	log.Info("setting up scheme")
	if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
		log.Error(err, "unable add APIs to scheme")
		os.Exit(1)
	}

	// initialize OPA
	// TODO: make tracing toggle-able
	driver := local.New(local.Tracing(true))
	backend, err := opa.NewBackend(opa.Driver(driver))
	if err != nil {
		log.Error(err, "unable to set up OPA backend")
		os.Exit(1)
	}
	client, err := backend.NewClient(opa.Targets(&target.K8sValidationTarget{}))
	if err != nil {
		log.Error(err, "unable to set up OPA client")
	}

	// Setup all Controllers
	log.Info("Setting up controller")
	if err := controller.AddToManager(mgr, client); err != nil {
		log.Error(err, "unable to register controllers to the manager")
		os.Exit(1)
	}

	log.Info("setting up webhooks")
	if err := webhook.AddToManager(mgr, client); err != nil {
		log.Error(err, "unable to register webhooks to the manager")
		os.Exit(1)
	}

	log.Info("setting up audit")
	if err := audit.AddToManager(mgr, client); err != nil {
		log.Error(err, "unable to register audit to the manager")
		os.Exit(1)
	}

	log.Info("setting up upgrade")
	if err := upgrade.AddToManager(mgr); err != nil {
		log.Error(err, "unable to register upgrade to the manager")
		os.Exit(1)
	}

	// Start the Cmd
	log.Info("Starting the Cmd.")
	if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
		log.Error(err, "unable to run the manager")
		os.Exit(1)
	}
}

func setLoggerForProduction() {
	sink := zapcore.AddSync(os.Stderr)
	var opts []zap.Option
	encCfg := zap.NewProductionEncoderConfig()
	enc := zapcore.NewJSONEncoder(encCfg)
	lvl := zap.NewAtomicLevelAt(zap.WarnLevel)
	opts = append(opts, zap.AddStacktrace(zap.ErrorLevel),
		zap.WrapCore(func(core zapcore.Core) zapcore.Core {
			return zapcore.NewSampler(core, time.Second, 100, 100)
		}))
	opts = append(opts, zap.AddCallerSkip(1), zap.ErrorOutput(sink))
	zlog := zap.New(zapcore.NewCore(&logf.KubeAwareEncoder{Encoder: enc, Verbose: false}, sink, lvl))
	zlog = zlog.WithOptions(opts...)
	newlogger := zapr.NewLogger(zlog)
	logf.SetLogger(newlogger)
}
<commit_msg>Do not always trace, for performance (#224)<commit_after>/*

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

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

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

package main

import (
	"flag"
	"os"
	"time"

	opa "github.com/open-policy-agent/frameworks/constraint/pkg/client"
	"github.com/open-policy-agent/frameworks/constraint/pkg/client/drivers/local"
	"github.com/open-policy-agent/gatekeeper/pkg/apis"
	"github.com/open-policy-agent/gatekeeper/pkg/audit"
	"github.com/open-policy-agent/gatekeeper/pkg/controller"
	"github.com/open-policy-agent/gatekeeper/pkg/target"
	"github.com/open-policy-agent/gatekeeper/pkg/upgrade"
	"github.com/open-policy-agent/gatekeeper/pkg/webhook"
	_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
	"sigs.k8s.io/controller-runtime/pkg/client/config"
	"sigs.k8s.io/controller-runtime/pkg/manager"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
	"sigs.k8s.io/controller-runtime/pkg/runtime/signals"

	"github.com/go-logr/zapr"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var (
	logLevel = flag.String("log-level", "INFO", "Minimum log level. For example, DEBUG, INFO, WARNING, ERROR. Defaulted to INFO if unspecified.")
)

func main() {

	flag.Parse()
	switch *logLevel {
	case "DEBUG":
		logf.SetLogger(logf.ZapLogger(true))
	case "WARNING", "ERROR":
		setLoggerForProduction()
	case "INFO":
		fallthrough
	default:
		logf.SetLogger(logf.ZapLogger(false))
	}

	log := logf.Log.WithName("entrypoint")

	// Get a config to talk to the apiserver
	log.Info("setting up client for manager")
	cfg, err := config.GetConfig()
	if err != nil {
		log.Error(err, "unable to set up client config")
		os.Exit(1)
	}

	// Create a new Cmd to provide shared dependencies and start components
	log.Info("setting up manager")
	mgr, err := manager.New(cfg, manager.Options{})
	if err != nil {
		log.Error(err, "unable to set up overall controller manager")
		os.Exit(1)
	}

	log.Info("Registering Components.")

	// Setup Scheme for all resources
	log.Info("setting up scheme")
	if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
		log.Error(err, "unable add APIs to scheme")
		os.Exit(1)
	}

	// initialize OPA
	driver := local.New(local.Tracing(false))
	backend, err := opa.NewBackend(opa.Driver(driver))
	if err != nil {
		log.Error(err, "unable to set up OPA backend")
		os.Exit(1)
	}
	client, err := backend.NewClient(opa.Targets(&target.K8sValidationTarget{}))
	if err != nil {
		log.Error(err, "unable to set up OPA client")
	}

	// Setup all Controllers
	log.Info("Setting up controller")
	if err := controller.AddToManager(mgr, client); err != nil {
		log.Error(err, "unable to register controllers to the manager")
		os.Exit(1)
	}

	log.Info("setting up webhooks")
	if err := webhook.AddToManager(mgr, client); err != nil {
		log.Error(err, "unable to register webhooks to the manager")
		os.Exit(1)
	}

	log.Info("setting up audit")
	if err := audit.AddToManager(mgr, client); err != nil {
		log.Error(err, "unable to register audit to the manager")
		os.Exit(1)
	}

	log.Info("setting up upgrade")
	if err := upgrade.AddToManager(mgr); err != nil {
		log.Error(err, "unable to register upgrade to the manager")
		os.Exit(1)
	}

	// Start the Cmd
	log.Info("Starting the Cmd.")
	if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
		log.Error(err, "unable to run the manager")
		os.Exit(1)
	}
}

func setLoggerForProduction() {
	sink := zapcore.AddSync(os.Stderr)
	var opts []zap.Option
	encCfg := zap.NewProductionEncoderConfig()
	enc := zapcore.NewJSONEncoder(encCfg)
	lvl := zap.NewAtomicLevelAt(zap.WarnLevel)
	opts = append(opts, zap.AddStacktrace(zap.ErrorLevel),
		zap.WrapCore(func(core zapcore.Core) zapcore.Core {
			return zapcore.NewSampler(core, time.Second, 100, 100)
		}))
	opts = append(opts, zap.AddCallerSkip(1), zap.ErrorOutput(sink))
	zlog := zap.New(zapcore.NewCore(&logf.KubeAwareEncoder{Encoder: enc, Verbose: false}, sink, lvl))
	zlog = zlog.WithOptions(opts...)
	newlogger := zapr.NewLogger(zlog)
	logf.SetLogger(newlogger)
}
<|endoftext|>


<commit_before>package main

import (
	"context"
	"flag"
	"fmt"
	"os"
	"runtime"

	// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
	_ "k8s.io/client-go/plugin/pkg/client/auth"

	"github.com/instaclustr/cassandra-operator/pkg/apis"
	"github.com/instaclustr/cassandra-operator/pkg/controller"

	"github.com/operator-framework/operator-sdk/pkg/k8sutil"
	"github.com/operator-framework/operator-sdk/pkg/leader"
	"github.com/operator-framework/operator-sdk/pkg/log/zap"
	"github.com/operator-framework/operator-sdk/pkg/metrics"
	sdkVersion "github.com/operator-framework/operator-sdk/version"
	"github.com/spf13/pflag"
	"sigs.k8s.io/controller-runtime/pkg/client/config"
	"sigs.k8s.io/controller-runtime/pkg/manager"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
	"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
)

// Change below variables to serve metrics on different host or port.
var (
	metricsHost       = "0.0.0.0"
	metricsPort int32 = 8383
)
var log = logf.Log.WithName("cmd")

func printVersion() {
	log.Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
	log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
	log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version))
}

func main() {
	// Add the zap logger flag set to the CLI. The flag set must
	// be added before calling pflag.Parse().
	pflag.CommandLine.AddFlagSet(zap.FlagSet())

	// Add flags registered by imported packages (e.g. glog and
	// controller-runtime)
	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)

	pflag.Parse()

	// Use a zap logr.Logger implementation. If none of the zap
	// flags are configured (or if the zap flag set is not being
	// used), this defaults to a production zap logger.
	//
	// The logger instantiated here can be changed to any logger
	// implementing the logr.Logger interface. This logger will
	// be propagated through the whole operator, generating
	// uniform and structured logs.
	logf.SetLogger(zap.Logger())

	printVersion()

	namespace, err := k8sutil.GetWatchNamespace()
	if err != nil {
		log.Error(err, "Failed to get watch namespace")
		os.Exit(1)
	}

	// Get a config to talk to the apiserver
	cfg, err := config.GetConfig()
	if err != nil {
		log.Error(err, "")
		os.Exit(1)
	}

	ctx := context.TODO()

	// Become the leader before proceeding
	err = leader.Become(ctx, "cassandra-operator/-lock")
	if err != nil {
		log.Error(err, "")
		os.Exit(1)
	}

	// Create a new Cmd to provide shared dependencies and start components
	mgr, err := manager.New(cfg, manager.Options{
		Namespace:          namespace,
		MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
	})
	if err != nil {
		log.Error(err, "")
		os.Exit(1)
	}

	log.Info("Registering Components.")

	// Setup Scheme for all resources
	if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
		log.Error(err, "")
		os.Exit(1)
	}

	// Setup all Controllers
	if err := controller.AddToManager(mgr); err != nil {
		log.Error(err, "")
		os.Exit(1)
	}

	// Create Service object to expose the metrics port.
	_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
	if err != nil {
		log.Info(err.Error())
	}

	log.Info("Starting the Cmd.")

	// Start the Cmd
	if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
		log.Error(err, "Manager exited non-zero")
		os.Exit(1)
	}
}
<commit_msg>Remove slash from leader ConfigMap name<commit_after>package main

import (
	"context"
	"flag"
	"fmt"
	"os"
	"runtime"

	// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
	_ "k8s.io/client-go/plugin/pkg/client/auth"

	"github.com/instaclustr/cassandra-operator/pkg/apis"
	"github.com/instaclustr/cassandra-operator/pkg/controller"

	"github.com/operator-framework/operator-sdk/pkg/k8sutil"
	"github.com/operator-framework/operator-sdk/pkg/leader"
	"github.com/operator-framework/operator-sdk/pkg/log/zap"
	"github.com/operator-framework/operator-sdk/pkg/metrics"
	sdkVersion "github.com/operator-framework/operator-sdk/version"
	"github.com/spf13/pflag"
	"sigs.k8s.io/controller-runtime/pkg/client/config"
	"sigs.k8s.io/controller-runtime/pkg/manager"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
	"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
)

// Change below variables to serve metrics on different host or port.
var (
	metricsHost       = "0.0.0.0"
	metricsPort int32 = 8383
)
var log = logf.Log.WithName("cmd")

func printVersion() {
	log.Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
	log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
	log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version))
}

func main() {
	// Add the zap logger flag set to the CLI. The flag set must
	// be added before calling pflag.Parse().
	pflag.CommandLine.AddFlagSet(zap.FlagSet())

	// Add flags registered by imported packages (e.g. glog and
	// controller-runtime)
	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)

	pflag.Parse()

	// Use a zap logr.Logger implementation. If none of the zap
	// flags are configured (or if the zap flag set is not being
	// used), this defaults to a production zap logger.
	//
	// The logger instantiated here can be changed to any logger
	// implementing the logr.Logger interface. This logger will
	// be propagated through the whole operator, generating
	// uniform and structured logs.
	logf.SetLogger(zap.Logger())

	printVersion()

	namespace, err := k8sutil.GetWatchNamespace()
	if err != nil {
		log.Error(err, "Failed to get watch namespace")
		os.Exit(1)
	}

	// Get a config to talk to the apiserver
	cfg, err := config.GetConfig()
	if err != nil {
		log.Error(err, "")
		os.Exit(1)
	}

	ctx := context.TODO()

	// Become the leader before proceeding
	err = leader.Become(ctx, "cassandra-operator-lock")
	if err != nil {
		log.Error(err, "")
		os.Exit(1)
	}

	// Create a new Cmd to provide shared dependencies and start components
	mgr, err := manager.New(cfg, manager.Options{
		Namespace:          namespace,
		MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
	})
	if err != nil {
		log.Error(err, "")
		os.Exit(1)
	}

	log.Info("Registering Components.")

	// Setup Scheme for all resources
	if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
		log.Error(err, "")
		os.Exit(1)
	}

	// Setup all Controllers
	if err := controller.AddToManager(mgr); err != nil {
		log.Error(err, "")
		os.Exit(1)
	}

	// Create Service object to expose the metrics port.
	_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
	if err != nil {
		log.Info(err.Error())
	}

	log.Info("Starting the Cmd.")

	// Start the Cmd
	if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
		log.Error(err, "Manager exited non-zero")
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/Symantec/Dominator/lib/mdb"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
	"log"
)

type awsGeneratorType struct {
	svc *ec2.EC2
}

func newAwsGenerator(
	datacentre, profile string) (
	result *awsGeneratorType, err error) {
	sess, err := session.NewSessionWithOptions(
		session.Options{
			Config:  aws.Config{Region: aws.String(datacentre)},
			Profile: profile,
		})
	if err != nil {
		return
	}
	svc := ec2.New(sess)
	result = &awsGeneratorType{svc: svc}
	return
}

func (g *awsGeneratorType) Generate(
	unused_datacentre string, unused_logger *log.Logger) (
	result *mdb.Mdb, err error) {
	params := &ec2.DescribeInstancesInput{
		Filters: []*ec2.Filter{
			{
				Name:   aws.String("instance-state-name"),
				Values: []*string{aws.String("running")},
			},
		},
	}
	resp, err := g.svc.DescribeInstances(params)
	if err != nil {
		return
	}
	result = extractMdb(resp)
	return
}

func extractMdb(output *ec2.DescribeInstancesOutput) *mdb.Mdb {
	var result mdb.Mdb
	for _, reservation := range output.Reservations {
		for _, instance := range reservation.Instances {
			if instance.PrivateDnsName != nil {
				machine := mdb.Machine{Hostname: *instance.PrivateDnsName}
				extractTags(instance.Tags, &machine)
				result.Machines = append(result.Machines, machine)
			}
		}
	}
	return &result
}

func extractTags(tags []*ec2.Tag, machine *mdb.Machine) {
	for _, tag := range tags {
		switch *tag.Key {
		case "RequiredImage":
			if tag.Value != nil {
				machine.RequiredImage = *tag.Value
			}
		case "PlannedImage":
			if tag.Value != nil {
				machine.PlannedImage = *tag.Value
			}
		case "DisableUpdates":
			if tag.Value != nil {
				machine.DisableUpdates = true
			}
		case "OwnerGroup":
			if tag.Value != nil {
				machine.OwnerGroup = *tag.Value
			}
		}
	}
}
<commit_msg>Populate IpAddress field in mdbd AWS driver.<commit_after>package main

import (
	"github.com/Symantec/Dominator/lib/mdb"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
	"log"
)

type awsGeneratorType struct {
	svc *ec2.EC2
}

func newAwsGenerator(
	datacentre, profile string) (
	result *awsGeneratorType, err error) {
	sess, err := session.NewSessionWithOptions(
		session.Options{
			Config:  aws.Config{Region: aws.String(datacentre)},
			Profile: profile,
		})
	if err != nil {
		return
	}
	svc := ec2.New(sess)
	result = &awsGeneratorType{svc: svc}
	return
}

func (g *awsGeneratorType) Generate(
	unused_datacentre string, unused_logger *log.Logger) (
	result *mdb.Mdb, err error) {
	params := &ec2.DescribeInstancesInput{
		Filters: []*ec2.Filter{
			{
				Name:   aws.String("instance-state-name"),
				Values: []*string{aws.String("running")},
			},
		},
	}
	resp, err := g.svc.DescribeInstances(params)
	if err != nil {
		return
	}
	result = extractMdb(resp)
	return
}

func extractMdb(output *ec2.DescribeInstancesOutput) *mdb.Mdb {
	var result mdb.Mdb
	for _, reservation := range output.Reservations {
		for _, instance := range reservation.Instances {
			if instance.PrivateDnsName != nil {
				machine := mdb.Machine{Hostname: *instance.PrivateDnsName}
				if instance.PrivateIpAddress != nil {
					machine.IpAddress = *instance.PrivateIpAddress
				}
				extractTags(instance.Tags, &machine)
				result.Machines = append(result.Machines, machine)
			}
		}
	}
	return &result
}

func extractTags(tags []*ec2.Tag, machine *mdb.Machine) {
	for _, tag := range tags {
		switch *tag.Key {
		case "RequiredImage":
			if tag.Value != nil {
				machine.RequiredImage = *tag.Value
			}
		case "PlannedImage":
			if tag.Value != nil {
				machine.PlannedImage = *tag.Value
			}
		case "DisableUpdates":
			if tag.Value != nil {
				machine.DisableUpdates = true
			}
		case "OwnerGroup":
			if tag.Value != nil {
				machine.OwnerGroup = *tag.Value
			}
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"code.google.com/p/go.net/proxy"
	"flag"
	"fmt"
	"github.com/calmh/mole/ansi"
	"github.com/calmh/mole/conf"
	"io/ioutil"
	"os"
	"time"
)

func init() {
	commands["dig"] = command{commandDig, msgDigShort}
}

const keepaliveInterval = 45 * time.Second

func commandDig(args []string) {
	fs := flag.NewFlagSet("dig", flag.ExitOnError)
	local := fs.Bool("l", false, "Local file, not remote tunnel definition")
	qualify := fs.Bool("q", false, "Use <host>.<tunnel> for host aliases instead of just <host>")
	fs.Usage = usageFor(fs, msgDigUsage)
	fs.Parse(args)
	args = fs.Args()

	if len(args) != 1 {
		fs.Usage()
		os.Exit(3)
	}

	// Fail early in case we don't have root since it's always required on
	// platforms where it matters
	requireRoot("dig")

	var err error

	cl := NewClient(serverAddress(), moleIni.Get("server", "fingerprint"))
	_, err = authenticated(cl, func() (interface{}, error) { return cl.Ping() })
	fatalErr(err)

	var tun string
	if *local {
		fd, err := os.Open(args[0])
		fatalErr(err)
		bs, err := ioutil.ReadAll(fd)
		fatalErr(err)
		tun, err = cl.Deobfuscate(string(bs))
		fatalErr(err)
	} else {
		tun, err = cl.Get(args[0])
		fatalErr(err)
	}

	tun, err = cl.Deobfuscate(tun)
	fatalErr(err)
	cfg, err := conf.Load(bytes.NewBufferString(tun))
	fatalErr(err)

	if cfg == nil {
		fatalln("no tunnel loaded")
	}

	for _, cmt := range cfg.Comments {
		infoln(ansi.Cyan("; " + cmt))
	}
	for _, cmt := range cfg.General.Comments {
		infoln(ansi.Cyan("; " + cmt))
	}

	var addrs []string
	if remapIntfs {
		cfg.Remap()
	} else {
		addrs = missingAddresses(cfg)
		if len(addrs) > 0 {
			addAddresses(addrs)
		}
	}

	infoln(sshPathStr(cfg.General.Main, cfg), "...")

	var vpn VPN
	if cfg.Vpnc != nil {
		vpn, err = startVpn("vpnc", cfg)
		fatalErr(err)
	} else if cfg.OpenConnect != nil {
		vpn, err = startVpn("openconnect", cfg)
		fatalErr(err)
	}

	var dialer Dialer = proxy.Direct
	if mh := cfg.General.Main; mh != "" {
		sshConn, err := sshHost(mh, cfg)
		fatalErr(err)
		dialer = sshConn

		go func() {
			// Periodically connect to a forward to provide a primitive keepalive mechanism.
			i := 0
			for {
				for _, fwd := range cfg.Forwards {
					for _, line := range fwd.Lines {
						time.Sleep(keepaliveInterval)
						go func(line conf.ForwardLine) {
							x := 0
							if line.Repeat > 0 {
								x = i % line.Repeat
							}
							debugln("keepalive dial", line.DstString(x))
							conn, err := dialer.Dial("tcp", line.DstString(x))
							if err != nil {
								debugln("keepalive dial", err)
							}
							if conn != nil {
								conn.Close()
							}
						}(line)
					}
				}
				i++
			}
		}()
	}

	fwdChan := startForwarder(dialer)
	sendForwards(fwdChan, cfg)

	setupHostsFile(args[0], cfg, *qualify)

	shell(fwdChan, cfg, dialer)

	restoreHostsFile(args[0], *qualify)

	if vpn != nil {
		vpn.Stop()
	}

	if !remapIntfs {
		addrs = extraneousAddresses(cfg)
		if len(addrs) > 0 {
			removeAddresses(addrs)
		}
	}

	okln("Done")
	printTotalStats()
}

func sendForwards(fwdChan chan<- conf.ForwardLine, cfg *conf.Config) {
	for _, fwd := range cfg.Forwards {
		infoln(ansi.Bold(ansi.Cyan(fwd.Name)))
		for _, cmt := range fwd.Comments {
			infoln(ansi.Cyan("  ; " + cmt))
		}
		for _, line := range fwd.Lines {
			infoln("  " + line.String())
			fwdChan <- line
		}
	}
}

func sshPathStr(hostname string, cfg *conf.Config) string {
	var this string
	if hostID, ok := cfg.HostsMap[hostname]; ok {
		host := cfg.Hosts[hostID]
		this = fmt.Sprintf("ssh://%s@%s", host.User, host.Name)

		if host.Via != "" {
			this = sshPathStr(host.Via, cfg) + " -> " + this
		}

		if host.SOCKS != "" {
			this = "SOCKS://" + host.SOCKS + " -> " + this
		}
	}

	if hostname == cfg.General.Main || hostname == "" {
		if cfg.Vpnc != nil {
			vpnc := fmt.Sprintf("vpnc://%s", cfg.Vpnc["IPSec_gateway"])
			if this == "" {
				return vpnc
			} else {
				this = vpnc + " -> " + this
			}
		} else if cfg.OpenConnect != nil {
			opnc := fmt.Sprintf("openconnect://%s", cfg.OpenConnect["server"])
			if this == "" {
				return opnc
			} else {
				this = opnc + " -> " + this
			}
		}
	}

	return this
}
<commit_msg>Don't require server for entirely local digs<commit_after>package main

import (
	"bytes"
	"code.google.com/p/go.net/proxy"
	"flag"
	"fmt"
	"github.com/calmh/mole/ansi"
	"github.com/calmh/mole/conf"
	"io/ioutil"
	"os"
	"time"
)

func init() {
	commands["dig"] = command{commandDig, msgDigShort}
}

const keepaliveInterval = 45 * time.Second

func commandDig(args []string) {
	fs := flag.NewFlagSet("dig", flag.ExitOnError)
	local := fs.Bool("l", false, "Local file, not remote tunnel definition")
	qualify := fs.Bool("q", false, "Use <host>.<tunnel> for host aliases instead of just <host>")
	fs.Usage = usageFor(fs, msgDigUsage)
	fs.Parse(args)
	args = fs.Args()

	if len(args) != 1 {
		fs.Usage()
		os.Exit(3)
	}

	// Fail early in case we don't have root since it's always required on
	// platforms where it matters
	requireRoot("dig")

	var err error

	cl := NewClient(serverAddress(), moleIni.Get("server", "fingerprint"))

	var tun string
	if *local {
		fd, err := os.Open(args[0])
		fatalErr(err)
		bs, err := ioutil.ReadAll(fd)
		fatalErr(err)
		tuni, err := authenticated(cl, func() (interface{}, error) { return cl.Deobfuscate(string(bs)) })
		fatalErr(err)
		tun = tuni.(string)
	} else {
		tuni, err := authenticated(cl, func() (interface{}, error) { return cl.Get(args[0]) })
		fatalErr(err)
		tun = tuni.(string)
		tun, err = cl.Deobfuscate(tun)
		fatalErr(err)
	}

	cfg, err := conf.Load(bytes.NewBufferString(tun))
	fatalErr(err)

	if cfg == nil {
		fatalln("no tunnel loaded")
	}

	for _, cmt := range cfg.Comments {
		infoln(ansi.Cyan("; " + cmt))
	}
	for _, cmt := range cfg.General.Comments {
		infoln(ansi.Cyan("; " + cmt))
	}

	var addrs []string
	if remapIntfs {
		cfg.Remap()
	} else {
		addrs = missingAddresses(cfg)
		if len(addrs) > 0 {
			addAddresses(addrs)
		}
	}

	infoln(sshPathStr(cfg.General.Main, cfg), "...")

	var vpn VPN
	if cfg.Vpnc != nil {
		vpn, err = startVpn("vpnc", cfg)
		fatalErr(err)
	} else if cfg.OpenConnect != nil {
		vpn, err = startVpn("openconnect", cfg)
		fatalErr(err)
	}

	var dialer Dialer = proxy.Direct
	if mh := cfg.General.Main; mh != "" {
		sshConn, err := sshHost(mh, cfg)
		fatalErr(err)
		dialer = sshConn

		go func() {
			// Periodically connect to a forward to provide a primitive keepalive mechanism.
			i := 0
			for {
				for _, fwd := range cfg.Forwards {
					for _, line := range fwd.Lines {
						time.Sleep(keepaliveInterval)
						go func(line conf.ForwardLine) {
							x := 0
							if line.Repeat > 0 {
								x = i % line.Repeat
							}
							debugln("keepalive dial", line.DstString(x))
							conn, err := dialer.Dial("tcp", line.DstString(x))
							if err != nil {
								debugln("keepalive dial", err)
							}
							if conn != nil {
								conn.Close()
							}
						}(line)
					}
				}
				i++
			}
		}()
	}

	fwdChan := startForwarder(dialer)
	sendForwards(fwdChan, cfg)

	setupHostsFile(args[0], cfg, *qualify)

	shell(fwdChan, cfg, dialer)

	restoreHostsFile(args[0], *qualify)

	if vpn != nil {
		vpn.Stop()
	}

	if !remapIntfs {
		addrs = extraneousAddresses(cfg)
		if len(addrs) > 0 {
			removeAddresses(addrs)
		}
	}

	okln("Done")
	printTotalStats()
}

func sendForwards(fwdChan chan<- conf.ForwardLine, cfg *conf.Config) {
	for _, fwd := range cfg.Forwards {
		infoln(ansi.Bold(ansi.Cyan(fwd.Name)))
		for _, cmt := range fwd.Comments {
			infoln(ansi.Cyan("  ; " + cmt))
		}
		for _, line := range fwd.Lines {
			infoln("  " + line.String())
			fwdChan <- line
		}
	}
}

func sshPathStr(hostname string, cfg *conf.Config) string {
	var this string
	if hostID, ok := cfg.HostsMap[hostname]; ok {
		host := cfg.Hosts[hostID]
		this = fmt.Sprintf("ssh://%s@%s", host.User, host.Name)

		if host.Via != "" {
			this = sshPathStr(host.Via, cfg) + " -> " + this
		}

		if host.SOCKS != "" {
			this = "SOCKS://" + host.SOCKS + " -> " + this
		}
	}

	if hostname == cfg.General.Main || hostname == "" {
		if cfg.Vpnc != nil {
			vpnc := fmt.Sprintf("vpnc://%s", cfg.Vpnc["IPSec_gateway"])
			if this == "" {
				return vpnc
			} else {
				this = vpnc + " -> " + this
			}
		} else if cfg.OpenConnect != nil {
			opnc := fmt.Sprintf("openconnect://%s", cfg.OpenConnect["server"])
			if this == "" {
				return opnc
			} else {
				this = opnc + " -> " + this
			}
		}
	}

	return this
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"image"
	"image/jpeg"
	"log"
	"os"

	"github.com/donatj/mpo"
)

var (
	format = flag.String("format", "stereo", "Output format [stereo|red-cyan|cyan-red|red-green|green-red]")
	output = flag.String("outfile", "output.jpg", "Output filename")
	help   = flag.Bool("help", false, "Displays this text")
)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n <mpofile>", os.Args[0])
		flag.PrintDefaults()
	}

	flag.Parse()

	if flag.NArg() != 1 {
		flag.Usage()
		os.Exit(2)
	}
}

func main() {
	r, err := os.Open(flag.Arg(0))
	if err != nil {
		log.Fatalf("err on %v %s", err, flag.Arg(0))
	}

	m, err := mpo.DecodeAll(r)
	if err != nil {
		log.Fatalf("err on %v %s", err, flag.Arg(0))
	}

	var img image.Image
	switch *format {
	case "stereo":
		img = m.ConvertToStereo()
	case "red-cyan":
		img, err = m.ConvertToAnaglyph(mpo.RedCyan)
		if err != nil {
			log.Fatal(err)
		}
	case "cyan-red":
		img, err = m.ConvertToAnaglyph(mpo.CyanRed)
		if err != nil {
			log.Fatal(err)
		}
	case "red-green":
		img, err = m.ConvertToAnaglyph(mpo.RedGreen)
		if err != nil {
			log.Fatal(err)
		}
	case "green-red":
		img, err = m.ConvertToAnaglyph(mpo.GreenRed)
		if err != nil {
			log.Fatal(err)
		}
	default:
		log.Fatal("Unknown format:", *format)
	}

	f, err := os.OpenFile(*output, os.O_CREATE|os.O_WRONLY, 0666)
	if err != nil {
		log.Fatal(err)
	}

	if err = jpeg.Encode(f, img, nil); err != nil {
		log.Fatal(err)
	}
}
<commit_msg>Fixes slight usage weirdness<commit_after>package main

import (
	"flag"
	"fmt"
	"image"
	"image/jpeg"
	"log"
	"os"

	"github.com/donatj/mpo"
)

var (
	format = flag.String("format", "stereo", "Output format [stereo|red-cyan|cyan-red|red-green|green-red]")
	output = flag.String("outfile", "output.jpg", "Output filename")
	help   = flag.Bool("help", false, "Displays this text")
)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s <mpofile>:\n", os.Args[0])
		flag.PrintDefaults()
	}

	flag.Parse()

	if flag.NArg() != 1 {
		flag.Usage()
		os.Exit(2)
	}
}

func main() {
	r, err := os.Open(flag.Arg(0))
	if err != nil {
		log.Fatalf("err on %v %s", err, flag.Arg(0))
	}

	m, err := mpo.DecodeAll(r)
	if err != nil {
		log.Fatalf("err on %v %s", err, flag.Arg(0))
	}

	var img image.Image
	switch *format {
	case "stereo":
		img = m.ConvertToStereo()
	case "red-cyan":
		img, err = m.ConvertToAnaglyph(mpo.RedCyan)
		if err != nil {
			log.Fatal(err)
		}
	case "cyan-red":
		img, err = m.ConvertToAnaglyph(mpo.CyanRed)
		if err != nil {
			log.Fatal(err)
		}
	case "red-green":
		img, err = m.ConvertToAnaglyph(mpo.RedGreen)
		if err != nil {
			log.Fatal(err)
		}
	case "green-red":
		img, err = m.ConvertToAnaglyph(mpo.GreenRed)
		if err != nil {
			log.Fatal(err)
		}
	default:
		log.Fatal("Unknown format:", *format)
	}

	f, err := os.OpenFile(*output, os.O_CREATE|os.O_WRONLY, 0666)
	if err != nil {
		log.Fatal(err)
	}

	if err = jpeg.Encode(f, img, nil); err != nil {
		log.Fatal(err)
	}
}
<|endoftext|>


<commit_before>// Copyright 2016 The nvim-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 main

import (
	"context"
	"flag"
	logpkg "log"
	_ "net/http/pprof"
	"os"
	"os/signal"
	"syscall"

	"cloud.google.com/go/errorreporting"
	"cloud.google.com/go/profiler"
	"contrib.go.opencensus.io/exporter/ocagent"
	"contrib.go.opencensus.io/exporter/stackdriver"
	gops "github.com/google/gops/agent"
	"github.com/neovim/go-client/nvim/plugin"
	"github.com/pkg/errors"
	"go.opencensus.io/stats/view"
	"go.opencensus.io/trace"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	xerrors "golang.org/x/exp/errors"
	xfmt "golang.org/x/exp/errors/fmt"
	"golang.org/x/sync/errgroup"

	"github.com/zchee/nvim-go/pkg/autocmd"
	"github.com/zchee/nvim-go/pkg/buildctxt"
	"github.com/zchee/nvim-go/pkg/command"
	"github.com/zchee/nvim-go/pkg/config"
	"github.com/zchee/nvim-go/pkg/logger"
	"github.com/zchee/nvim-go/pkg/server"
	"github.com/zchee/nvim-go/pkg/version"
)

const (
	appName = "nvim-go"
)

// flags
var (
	fVersion    = flag.Bool("version", false, "Show the version information.")
	pluginHost  = flag.String("manifest", "", "Write plugin manifest for `host` to stdout")
	vimFilePath = flag.String("location", "", "Manifest is automatically written to `.vim file`")
)

func init() {
	flag.Parse()
	logpkg.SetPrefix("nvim-go: ")
}

func main() {
	if *fVersion {
		xfmt.Printf("%s:\n  version: %s\n", appName, version.Version)
		return
	}

	ctx, cancel := context.WithCancel(Context())
	defer cancel()

	if *pluginHost != "" {
		os.Unsetenv("NVIM_GO_DEBUG")               // disable zap output
		ctx = logger.NewContext(ctx, zap.NewNop()) // avoid nil panic on logger.FromContext

		fn := func(p *plugin.Plugin) error {
			return func(ctx context.Context, p *plugin.Plugin) error {
				ctx, cancel := context.WithCancel(ctx)
				defer cancel()
				bctxt := buildctxt.NewContext()
				c := command.Register(ctx, p, bctxt)
				autocmd.Register(ctx, cancel, p, bctxt, c)
				return nil
			}(ctx, p)
		}
		if err := Plugin(fn); err != nil {
			logpkg.Fatal(err)
		}
		return
	}

	env := config.Process()

	var lv zapcore.Level
	if err := lv.UnmarshalText([]byte(env.LogLevel)); err != nil {
		logpkg.Fatalf("failed to parse log level: %s, err: %v", env.LogLevel, err)
	}
	log, undo := logger.NewRedirectZapLogger(lv)
	defer undo()
	ctx = logger.NewContext(ctx, log)

	// Open socket for using gops to get stacktraces of the daemon.
	if err := gops.Listen(gops.Options{ConfigDir: "/tmp/gops", ShutdownCleanup: true}); err != nil {
		logpkg.Fatalf("unable to start gops: %s", err)
	}
	log.Info("starting gops agent")

	if config.HasGCPProjectID() {
		gcpProjectID := env.GCPProjectID
		trace.ApplyConfig(trace.Config{
			DefaultSampler: trace.AlwaysSample(),
		})

		// OpenCensus tracing with OpenCensus agent exporter
		oce, err := ocagent.NewExporter(ocagent.WithInsecure(), ocagent.WithServiceName(appName))
		if err != nil {
			msg := xerrors.New("Failed to create the OpenCensus agent exporter")
			err = xfmt.Errorf("%s: %v", msg, err)
			logpkg.Fatal(err)
		}
		defer oce.Stop()
		trace.RegisterExporter(oce)
		log.Info("opencensus", zap.String("trace", "enabled OpenCensus agent exporter"))

		// OpenCensus tracing with Stackdriver exporter
		sdOpts := stackdriver.Options{
			ProjectID: gcpProjectID,
			OnError: func(err error) {
				log.Error("stackdriver.Exporter", zap.Error(xfmt.Errorf("could not log error: %v", err)))
			},
			MetricPrefix: appName,
			Context:      ctx,
		}
		sd, err := stackdriver.NewExporter(sdOpts)
		if err != nil {
			logpkg.Fatalf("failed to create stackdriver exporter: %v", err)
		}
		defer sd.Flush()
		trace.RegisterExporter(sd)
		log.Info("opencensus", zap.String("trace", "enabled Stackdriver exporter"))
		view.RegisterExporter(sd)

		// Stackdriver Profiler
		profConf := profiler.Config{
			Service:        appName,
			ServiceVersion: version.Tag,
			MutexProfiling: true,
			ProjectID:      gcpProjectID,
		}
		if err := profiler.Start(profConf); err != nil {
			logpkg.Fatalf("failed to start stackdriver profiler: %v", err)
		}
		log.Info("stackdriver", zap.String("profiler", "enabled Stackdriver profiler"))

		// Stackdriver Error Reporting
		errReportConf := errorreporting.Config{
			ServiceName:    appName,
			ServiceVersion: version.Tag,
			OnError: func(err error) {
				log.Error("errorreporting", zap.Error(xfmt.Errorf("could not log error: %v", err)))
			},
		}
		errClient, err := errorreporting.NewClient(ctx, gcpProjectID, errReportConf)
		if err != nil {
			logpkg.Fatalf("failed to create errorreporting client: %v", err)
		}
		defer errClient.Close()
		ctx = context.WithValue(ctx, &errorreporting.Client{}, errClient)
		log.Info("stackdriver", zap.String("errorreporting", "enabled Stackdriver errorreporting"))

		var span *trace.Span
		ctx, span = trace.StartSpan(ctx, "main", trace.WithSampler(trace.AlwaysSample())) // start root span
		defer span.End()
	}

	log.Info(xfmt.Sprintf("starting %s server", appName), zap.Object("env", env))
	eg := new(errgroup.Group)
	eg, ctx = errgroup.WithContext(ctx)
	eg.Go(func() error {
		fn := func(p *plugin.Plugin) error {
			return Main(ctx, p)
		}
		return Plugin(fn)
	})
	eg.Go(func() error {
		return Child(ctx)
	})

	go func() {
		if err := eg.Wait(); err != nil {
			log.Fatal("eg.Wait", zap.Error(err))
		}
	}()

	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)

	select {
	case <-ctx.Done():
		log.Error("ctx.Done()", zap.Error(ctx.Err()))
		return
	case sig := <-sigc:
		switch sig {
		case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM:
			log.Info("catch signal", zap.String("name", sig.String()))
			return
		}
	}
}

func Main(ctx context.Context, p *plugin.Plugin) error {
	ctx, cancel := context.WithCancel(ctx)
	log := logger.FromContext(ctx).Named("main")
	ctx = logger.NewContext(ctx, log)

	bctxt := buildctxt.NewContext()
	autocmd.Register(ctx, cancel, p, bctxt, command.Register(ctx, p, bctxt))

	// switch to unix socket rpc-connection
	if n, err := server.Dial(ctx); err == nil {
		p.Nvim = n
	}

	return nil
}

func Child(ctx context.Context) error {
	log := logger.FromContext(ctx).Named("child")
	ctx = logger.NewContext(ctx, log)

	s, err := server.NewServer(ctx)
	if err != nil {
		return errors.Wrap(err, "failed to create NewServer")
	}
	go s.Serve()
	defer func() {
		if err := s.Close(); err != nil {
			log.Fatal("Close", zap.Error(err))
		}
	}()

	bufs, err := s.Buffers()
	if err != nil {
		return errors.Wrap(err, "failed to get buffers")
	}

	// Get the names using a single atomic call to Nvim.
	names := make([]string, len(bufs))
	b := s.NewBatch()
	for i, buf := range bufs {
		b.BufferName(buf, &names[i])
	}

	if err := b.Execute(); err != nil {
		return errors.Wrap(err, "failed to execute batch")
	}

	for _, name := range names {
		log.Info("buffer", zap.String("name", name))
	}

	return nil
}
<commit_msg>cmd/nvim-go: change Child func to only of subscribe<commit_after>// Copyright 2016 The nvim-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 main

import (
	"context"
	"flag"
	logpkg "log"
	_ "net/http/pprof"
	"os"
	"os/signal"
	"syscall"

	"cloud.google.com/go/errorreporting"
	"cloud.google.com/go/profiler"
	"contrib.go.opencensus.io/exporter/ocagent"
	"contrib.go.opencensus.io/exporter/stackdriver"
	gops "github.com/google/gops/agent"
	"github.com/neovim/go-client/nvim/plugin"
	"github.com/pkg/errors"
	"go.opencensus.io/stats/view"
	"go.opencensus.io/trace"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	xerrors "golang.org/x/exp/errors"
	xfmt "golang.org/x/exp/errors/fmt"
	"golang.org/x/sync/errgroup"

	"github.com/zchee/nvim-go/pkg/autocmd"
	"github.com/zchee/nvim-go/pkg/buildctxt"
	"github.com/zchee/nvim-go/pkg/command"
	"github.com/zchee/nvim-go/pkg/config"
	"github.com/zchee/nvim-go/pkg/logger"
	"github.com/zchee/nvim-go/pkg/server"
	"github.com/zchee/nvim-go/pkg/version"
)

const (
	appName = "nvim-go"
)

// flags
var (
	fVersion    = flag.Bool("version", false, "Show the version information.")
	pluginHost  = flag.String("manifest", "", "Write plugin manifest for `host` to stdout")
	vimFilePath = flag.String("location", "", "Manifest is automatically written to `.vim file`")
)

func init() {
	flag.Parse()
	logpkg.SetPrefix("nvim-go: ")
}

func main() {
	if *fVersion {
		xfmt.Printf("%s:\n  version: %s\n", appName, version.Version)
		return
	}

	ctx, cancel := context.WithCancel(Context())
	defer cancel()

	if *pluginHost != "" {
		os.Unsetenv("NVIM_GO_DEBUG")               // disable zap output
		ctx = logger.NewContext(ctx, zap.NewNop()) // avoid nil panic on logger.FromContext

		fn := func(p *plugin.Plugin) error {
			return func(ctx context.Context, p *plugin.Plugin) error {
				ctx, cancel := context.WithCancel(ctx)
				defer cancel()
				bctxt := buildctxt.NewContext()
				c := command.Register(ctx, p, bctxt)
				autocmd.Register(ctx, cancel, p, bctxt, c)
				return nil
			}(ctx, p)
		}
		if err := Plugin(fn); err != nil {
			logpkg.Fatal(err)
		}
		return
	}

	env := config.Process()

	var lv zapcore.Level
	if err := lv.UnmarshalText([]byte(env.LogLevel)); err != nil {
		logpkg.Fatalf("failed to parse log level: %s, err: %v", env.LogLevel, err)
	}
	log, undo := logger.NewRedirectZapLogger(lv)
	defer undo()
	ctx = logger.NewContext(ctx, log)

	// Open socket for using gops to get stacktraces of the daemon.
	if err := gops.Listen(gops.Options{ConfigDir: "/tmp/gops", ShutdownCleanup: true}); err != nil {
		logpkg.Fatalf("unable to start gops: %s", err)
	}
	log.Info("starting gops agent")

	if config.HasGCPProjectID() {
		gcpProjectID := env.GCPProjectID
		trace.ApplyConfig(trace.Config{
			DefaultSampler: trace.AlwaysSample(),
		})

		// OpenCensus tracing with OpenCensus agent exporter
		oce, err := ocagent.NewExporter(ocagent.WithInsecure(), ocagent.WithServiceName(appName))
		if err != nil {
			msg := xerrors.New("Failed to create the OpenCensus agent exporter")
			err = xfmt.Errorf("%s: %v", msg, err)
			logpkg.Fatal(err)
		}
		defer oce.Stop()
		trace.RegisterExporter(oce)
		log.Info("opencensus", zap.String("trace", "enabled OpenCensus agent exporter"))

		// OpenCensus tracing with Stackdriver exporter
		sdOpts := stackdriver.Options{
			ProjectID: gcpProjectID,
			OnError: func(err error) {
				log.Error("stackdriver.Exporter", zap.Error(xfmt.Errorf("could not log error: %v", err)))
			},
			MetricPrefix: appName,
			Context:      ctx,
		}
		sd, err := stackdriver.NewExporter(sdOpts)
		if err != nil {
			logpkg.Fatalf("failed to create stackdriver exporter: %v", err)
		}
		defer sd.Flush()
		trace.RegisterExporter(sd)
		log.Info("opencensus", zap.String("trace", "enabled Stackdriver exporter"))
		view.RegisterExporter(sd)

		// Stackdriver Profiler
		profConf := profiler.Config{
			Service:        appName,
			ServiceVersion: version.Tag,
			MutexProfiling: true,
			ProjectID:      gcpProjectID,
		}
		if err := profiler.Start(profConf); err != nil {
			logpkg.Fatalf("failed to start stackdriver profiler: %v", err)
		}
		log.Info("stackdriver", zap.String("profiler", "enabled Stackdriver profiler"))

		// Stackdriver Error Reporting
		errReportConf := errorreporting.Config{
			ServiceName:    appName,
			ServiceVersion: version.Tag,
			OnError: func(err error) {
				log.Error("errorreporting", zap.Error(xfmt.Errorf("could not log error: %v", err)))
			},
		}
		errClient, err := errorreporting.NewClient(ctx, gcpProjectID, errReportConf)
		if err != nil {
			logpkg.Fatalf("failed to create errorreporting client: %v", err)
		}
		defer errClient.Close()
		ctx = context.WithValue(ctx, &errorreporting.Client{}, errClient)
		log.Info("stackdriver", zap.String("errorreporting", "enabled Stackdriver errorreporting"))

		var span *trace.Span
		ctx, span = trace.StartSpan(ctx, "main", trace.WithSampler(trace.AlwaysSample())) // start root span
		defer span.End()
	}

	log.Info(xfmt.Sprintf("starting %s server", appName), zap.Object("env", env))
	eg := new(errgroup.Group)
	eg, ctx = errgroup.WithContext(ctx)
	eg.Go(func() error {
		fn := func(p *plugin.Plugin) error {
			return Main(ctx, p)
		}
		return Plugin(fn)
	})
	eg.Go(func() error {
		return Child(ctx)
	})

	go func() {
		if err := eg.Wait(); err != nil {
			log.Fatal("eg.Wait", zap.Error(err))
		}
	}()

	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)

	select {
	case <-ctx.Done():
		log.Error("ctx.Done()", zap.Error(ctx.Err()))
		return
	case sig := <-sigc:
		switch sig {
		case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM:
			log.Info("catch signal", zap.String("name", sig.String()))
			return
		}
	}
}

func Main(ctx context.Context, p *plugin.Plugin) error {
	ctx, cancel := context.WithCancel(ctx)
	log := logger.FromContext(ctx).Named("main")
	ctx = logger.NewContext(ctx, log)

	bctxt := buildctxt.NewContext()
	autocmd.Register(ctx, cancel, p, bctxt, command.Register(ctx, p, bctxt))

	// switch to unix socket rpc-connection
	if n, err := server.Dial(ctx); err == nil {
		p.Nvim = n
	}

	return nil
}

func Child(ctx context.Context) error {
	log := logger.FromContext(ctx).Named("child")
	ctx = logger.NewContext(ctx, log)

	s, err := server.NewServer(ctx)
	if err != nil {
		return errors.Wrap(err, "failed to create NewServer")
	}
	go s.Serve()

	s.Subscribe("NvimGo")

	select {
	case <-ctx.Done():
		if err := s.Close(); err != nil {
			log.Fatal("Close", zap.Error(err))
		}
		return nil
	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/pkg/errors"
	"github.com/scipipe/scipipe"
)

func main() {
	scipipe.InitLogError()

	flag.Parse()
	cmd := flag.Arg(0)
	switch cmd {
	case "new":
		if len(flag.Args()) < 2 {
			fmt.Println("ERROR: No filename specified!")
			printNewUsage()
			os.Exit(1)
		}
		writeNewWorkflowFile(flag.Arg(1))
	case "audit2html":
		if len(flag.Args()) < 2 {
			fmt.Println("ERROR: No infile specified!")
			printAudit2HTMLUsage()
			os.Exit(1)
		}
		inFile := flag.Arg(1)
		if len(flag.Args()) < 3 {
			fmt.Println("ERROR: No outfile specified!")
			printAudit2HTMLUsage()
			os.Exit(1)
		}
		outFile := flag.Arg(2)
		err := convertAudit2Html(inFile, outFile)
		if err != nil {
			scipipe.CheckWithMsg(err, "Could not convert Audit file to HTML")
		}
	default:
		printHelp()
	}
}

func printNewUsage() {
	fmt.Print(`
Usage:
$ scipipe new <filename.go>
`)
}
func printAudit2HTMLUsage() {
	fmt.Print(`
Usage:
$ scipipe audit2html <infile.audit.json> [<outfile.html>]
`)
}

func printHelp() {
	fmt.Printf(`________________________________________________________________________

SciPipe (http://scipipe.org)
Version: %s
________________________________________________________________________

Usage:
$ scipipe <command> [command options]

Available commands:
$ scipipe new <filename.go>
$ scipipe audit2html <infile.audit.json> [<outfile.html>]
________________________________________________________________________
`, scipipe.Version)
}

func writeNewWorkflowFile(fileName string) {
	wfcode := `// Workflow written in SciPipe.
// For more information about SciPipe, see: http://scipipe.org
package main

import sp "github.com/scipipe/scipipe"

func main() {
	// Create a workflow, using 4 cpu cores
	wf := sp.NewWorkflow("my_workflow", 4)

	// Initialize processes
	foo := wf.NewProc("fooer", "echo foo > {o:foo}")
	foo.SetPathStatic("foo", "foo.txt")

	f2b := wf.NewProc("foo2bar", "sed 's/foo/bar/g' {i:foo} > {o:bar}")
	f2b.SetPathExtend("foo", "bar", ".bar.txt")

	// From workflow dependency network
	f2b.In("foo").From(foo.Out("foo"))

	// Run the workflow
	wf.Run()
}`
	f, err := os.Create(fileName)
	if err != nil {
		scipipe.Fail("Could not create file:", fileName)
	}
	defer f.Close()
	_, err = f.Write([]byte(wfcode))
	if err != nil {
		scipipe.Fail("Could not write to file:", fileName)
	}
	fmt.Println("Successfully wrote new workflow file to:", fileName, "\n\nNow you can run it with:\ngo run ", fileName)
}

func convertAudit2Html(inFilePath string, outFilePath string) error {
	ip := scipipe.NewFileIP(strings.Replace(inFilePath, ".audit.json", "", 1))
	ai := ip.AuditInfo()

	outHTML := fmt.Sprintf(`<html><head><title>Audit info for: %s</title></head><body>`, ip.Path())
	outHTML += formatAuditInfoHTML(ip.Path(), ai)
	outHTML += `</body></html>`

	if _, err := os.Stat(outFilePath); os.IsExist(err) {
		return errors.Wrap(err, "File already exists:"+outFilePath)
	}
	outFile, err := os.Create(outFilePath)
	if err != nil {
		return errors.Wrap(err, "Could not create file:"+outFilePath)
	}
	outFile.WriteString(outHTML)
	outFile.Close()
	return nil
}

func formatAuditInfoHTML(fileName string, ai *scipipe.AuditInfo) (outHTML string) {
	outHTML = "
\n"
	outHTML += fmt.Sprintf(`<tr><td colspan="2" style="font-size: 1.2em; font-weight: bold; text-align: left; padding: .2em .4em; ">%s</td></tr>`, fileName)
	outHTML += fmt.Sprintf("
ID:	%s
\n", ai.ID)
	outHTML += fmt.Sprintf("
Process:	%s
\n", ai.ProcessName)
	outHTML += fmt.Sprintf("
Command:
%s

\n", ai.Command)

	params := []string{}
	for pname, p := range ai.Params {
		params = append(params, fmt.Sprintf("%s: %s", pname, p))
	}
	outHTML += fmt.Sprintf("
Parameters:	%s
\n", strings.Join(params, ", "))
	tags := []string{}
	for pname, p := range ai.Tags {
		tags = append(tags, fmt.Sprintf("%s: %s", pname, p))
	}
	outHTML += fmt.Sprintf("
Tags:
%v

\n", strings.Join(tags, ", "))

	outHTML += fmt.Sprintf("
Start time:	%v
\n", ai.StartTime)
	outHTML += fmt.Sprintf("
Finish time:	%v
\n", ai.FinishTime)
	outHTML += fmt.Sprintf("
Execution time:	%d ms
\n", ai.ExecTimeMS)
	upStreamHTML := ""
	for filePath, uai := range ai.Upstream {
		upStreamHTML += formatAuditInfoHTML(filePath, uai)
	}
	if outHTML != "" {
		outHTML += "
Upstreams:	" + upStreamHTML + "
\n"
	}
	outHTML += "
\n"
	return
}
<commit_msg>Refactor and simplify scipipe command<commit_after>package main

import (
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/pkg/errors"
	"github.com/scipipe/scipipe"
)

func main() {
	scipipe.InitLogError()

	flag.Parse()
	cmd := flag.Arg(0)
	switch cmd {
	case "new":
		if len(flag.Args()) < 2 {
			fmt.Println("ERROR: No filename specified!")
			printNewUsage()
			os.Exit(1)
		}
		writeNewWorkflowFile(flag.Arg(1))
	case "audit2html":
		if len(flag.Args()) < 2 {
			fmt.Println("ERROR: No infile specified!")
			printAudit2HTMLUsage()
			os.Exit(1)
		}
		inFile := flag.Arg(1)
		if len(flag.Args()) < 3 {
			fmt.Println("ERROR: No outfile specified!")
			printAudit2HTMLUsage()
			os.Exit(1)
		}
		outFile := flag.Arg(2)
		err := auditInfoToHTML(inFile, outFile)
		if err != nil {
			scipipe.CheckWithMsg(err, "Could not convert Audit file to HTML")
		}
	default:
		printHelp()
	}
}

func printNewUsage() {
	fmt.Print(`
Usage:
$ scipipe new <filename.go>
`)
}
func printAudit2HTMLUsage() {
	fmt.Print(`
Usage:
$ scipipe audit2html <infile.audit.json> [<outfile.html>]
`)
}

func printHelp() {
	fmt.Printf(`________________________________________________________________________

SciPipe (http://scipipe.org)
Version: %s
________________________________________________________________________

Usage:
$ scipipe <command> [command options]

Available commands:
$ scipipe new <filename.go>
$ scipipe audit2html <infile.audit.json> [<outfile.html>]
________________________________________________________________________
`, scipipe.Version)
}

func writeNewWorkflowFile(fileName string) {
	f, err := os.Create(fileName)
	if err != nil {
		scipipe.Fail("Could not create file:", fileName)
	}
	defer f.Close()
	_, err = f.Write([]byte(workflowStub))
	if err != nil {
		scipipe.Fail("Could not write to file:", fileName)
	}
	fmt.Println("Successfully wrote new workflow file to:", fileName, "\n\nNow you can run it with:\ngo run ", fileName)
}

func auditInfoToHTML(inFilePath string, outFilePath string) error {
	ip := scipipe.NewFileIP(strings.Replace(inFilePath, ".audit.json", "", 1))
	auditInfo := ip.AuditInfo()

	outHTML := fmt.Sprintf(`<html><head><title>Audit info for: %s</title></head><body>`, ip.Path())
	outHTML += formatTaskHTML(ip.Path(), auditInfo)
	outHTML += `</body></html>`

	if _, err := os.Stat(outFilePath); os.IsExist(err) {
		return errors.Wrap(err, "File already exists:"+outFilePath)
	}
	outFile, err := os.Create(outFilePath)
	if err != nil {
		return errors.Wrap(err, "Could not create file:"+outFilePath)
	}
	outFile.WriteString(outHTML)
	outFile.Close()
	return nil
}

func formatTaskHTML(fileName string, auditInfo *scipipe.AuditInfo) (outHTML string) {
	outHTML = "
\n"
	outHTML += fmt.Sprintf(`<tr><td colspan="2" style="font-size: 1.2em; font-weight: bold; text-align: left; padding: .2em .4em; ">%s</td></tr>`, fileName)
	outHTML += fmt.Sprintf("
ID:	%s
\n", auditInfo.ID)
	outHTML += fmt.Sprintf("
Process:	%s
\n", auditInfo.ProcessName)
	outHTML += fmt.Sprintf("
Command:
%s

\n", auditInfo.Command)

	params := []string{}
	for pname, p := range auditInfo.Params {
		params = append(params, fmt.Sprintf("%s: %s", pname, p))
	}
	outHTML += fmt.Sprintf("
Parameters:	%s
\n", strings.Join(params, ", "))
	tags := []string{}
	for pname, p := range auditInfo.Tags {
		tags = append(tags, fmt.Sprintf("%s: %s", pname, p))
	}
	outHTML += fmt.Sprintf("
Tags:
%v

\n", strings.Join(tags, ", "))

	outHTML += fmt.Sprintf("
Start time:	%v
\n", auditInfo.StartTime)
	outHTML += fmt.Sprintf("
Finish time:	%v
\n", auditInfo.FinishTime)
	outHTML += fmt.Sprintf("
Execution time:	%d ms
\n", auditInfo.ExecTimeMS)
	upStreamHTML := ""
	for filePath, uai := range auditInfo.Upstream {
		upStreamHTML += formatTaskHTML(filePath, uai)
	}
	if outHTML != "" {
		outHTML += "
Upstreams:	" + upStreamHTML + "
\n"
	}
	outHTML += "
\n"
	return
}

const (
	workflowStub = `// Workflow written in SciPipe.
// For more information about SciPipe, see: http://scipipe.org
package main

import sp "github.com/scipipe/scipipe"

func main() {
	// Create a workflow, using 4 cpu cores
	wf := sp.NewWorkflow("my_workflow", 4)

	// Initialize processes
	foo := wf.NewProc("fooer", "echo foo > {o:foo}")
	foo.SetPathStatic("foo", "foo.txt")

	f2b := wf.NewProc("foo2bar", "sed 's/foo/bar/g' {i:foo} > {o:bar}")
	f2b.SetPathExtend("foo", "bar", ".bar.txt")

	// From workflow dependency network
	f2b.In("foo").From(foo.Out("foo"))

	// Run the workflow
	wf.Run()
}`
)
<|endoftext|>


<commit_before>package main

import (
	"flag"

	"github.com/terranodo/tegola/server"
)

var (
	configFile string
	logFile    string
	logFormat  string
	port       string
)

const defaultHTTPPort = ":8080"

func init() {
	flag.StringVar(&configFile, "config", "config.toml", "Location of config file in TOML format. Can be absolute, relative or remote over http(s).")
	flag.StringVar(&logFile, "log-file", "", "The file to log output to. Disable by default.")
	flag.StringVar(&logFormat, "log-format", server.DefaultLogFormat,
		`The format that the logger will log with.

	Available fields:
		{{.Time}} : The current Date Time in RFC 2822 format.
		{{.RequestIP}} : The IP address of the the requester.
		{{.Z}} : The Zoom level.
		{{.X}} : The X Coordinate.
		{{.Y}} : The Y Coordinate.
`)
	flag.StringVar(&port, "port", defaultHTTPPort, "Webserver port to bind to")
}
<commit_msg>cli usage instructions<commit_after>package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/terranodo/tegola/server"
)

var (
	configFile = *flag.String("config", "config.toml", "")
	logFile    = *flag.String("log-file", "", "")
	logFormat  = *flag.String("log-format", server.DefaultLogFormat, "")
	port       = *flag.String("port", defaultHTTPPort, "")
)

const defaultHTTPPort = ":8080"

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, `tegola %v <http://tegola.io>
MVT tile server with support for PostGIS

USAGE:	tegola [OPTIONS]

OPTIONS:
  -h, --help                Print usage
  -v, --version             Print version and quit
      --config      string  Location of config file in TOML format. Can be local or remote over http(s) (default config.toml)
      --port        string  Port to bind HTTP server to (example :8080)
      --log-file    string  The file to write request logs to (default disabled)
      --log-format  string  The format the logger will log with. Available fields:
                                {{.Time}} : The current Date Time in RFC 2822 format
                                {{.RequestIP}} : The IP address of the the requester
                                {{.Z}} : The Zoom level
                                {{.X}} : The X Coordinate
                                {{.Y}} : The Y Coordinate

`, Version)
	}
}
<|endoftext|>


<commit_before>// Downloads torrents from the command-line.
package main

import (
	"expvar"
	"fmt"
	"net"
	"net/http"
	"os"
	"os/signal"
	"strings"
	"syscall"
	"time"

	"github.com/anacrolix/missinggo"
	"github.com/dustin/go-humanize"
	"golang.org/x/xerrors"

	"github.com/anacrolix/log"

	"github.com/anacrolix/envpprof"
	"github.com/anacrolix/tagflag"
	"golang.org/x/time/rate"

	"github.com/anacrolix/torrent"
	"github.com/anacrolix/torrent/iplist"
	"github.com/anacrolix/torrent/metainfo"
	"github.com/anacrolix/torrent/storage"
)

func torrentBar(t *torrent.Torrent, pieceStates bool) {
	go func() {
		if t.Info() == nil {
			fmt.Printf("getting info for %q\n", t.Name())
			<-t.GotInfo()
		}
		var lastLine string
		for {
			var completedPieces, partialPieces int
			psrs := t.PieceStateRuns()
			for _, r := range psrs {
				if r.Complete {
					completedPieces += r.Length
				}
				if r.Partial {
					partialPieces += r.Length
				}
			}
			line := fmt.Sprintf(
				"downloading %q: %s/%s, %d/%d pieces completed (%d partial)\n",
				t.Name(),
				humanize.Bytes(uint64(t.BytesCompleted())),
				humanize.Bytes(uint64(t.Length())),
				completedPieces,
				t.NumPieces(),
				partialPieces,
			)
			if line != lastLine {
				lastLine = line
				os.Stdout.WriteString(line)
			}
			if pieceStates {
				fmt.Println(psrs)
			}
			time.Sleep(time.Second)
		}
	}()
}

func addTorrents(client *torrent.Client) error {
	for _, arg := range flags.Torrent {
		t, err := func() (*torrent.Torrent, error) {
			if strings.HasPrefix(arg, "magnet:") {
				t, err := client.AddMagnet(arg)
				if err != nil {
					return nil, xerrors.Errorf("error adding magnet: %w", err)
				}
				return t, nil
			} else if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
				response, err := http.Get(arg)
				if err != nil {
					return nil, xerrors.Errorf("Error downloading torrent file: %s", err)
				}

				metaInfo, err := metainfo.Load(response.Body)
				defer response.Body.Close()
				if err != nil {
					return nil, xerrors.Errorf("error loading torrent file %q: %s\n", arg, err)
				}
				t, err := client.AddTorrent(metaInfo)
				if err != nil {
					return nil, xerrors.Errorf("adding torrent: %w", err)
				}
				return t, nil
			} else if strings.HasPrefix(arg, "infohash:") {
				t, _ := client.AddTorrentInfoHash(metainfo.NewHashFromHex(strings.TrimPrefix(arg, "infohash:")))
				return t, nil
			} else {
				metaInfo, err := metainfo.LoadFromFile(arg)
				if err != nil {
					return nil, xerrors.Errorf("error loading torrent file %q: %s\n", arg, err)
				}
				t, err := client.AddTorrent(metaInfo)
				if err != nil {
					return nil, xerrors.Errorf("adding torrent: %w", err)
				}
				return t, nil
			}
		}()
		if err != nil {
			return xerrors.Errorf("adding torrent for %q: %w", arg, err)
		}
		if flags.Progress {
			torrentBar(t, flags.PieceStates)
		}
		t.AddPeers(func() (ret []torrent.PeerInfo) {
			for _, ta := range flags.TestPeer {
				ret = append(ret, torrent.PeerInfo{
					Addr: ta,
				})
			}
			return
		}())
		go func() {
			<-t.GotInfo()
			t.DownloadAll()
		}()
	}
	return nil
}

var flags = struct {
	Mmap            bool           `help:"memory-map torrent data"`
	TestPeer        []*net.TCPAddr `help:"addresses of some starting peers"`
	Seed            bool           `help:"seed after download is complete"`
	Addr            string         `help:"network listen addr"`
	UploadRate      tagflag.Bytes  `help:"max piece bytes to send per second"`
	DownloadRate    tagflag.Bytes  `help:"max bytes per second down from peers"`
	Debug           bool
	PackedBlocklist string
	Stats           *bool
	PublicIP        net.IP
	Progress        bool
	PieceStates     bool
	Quiet           bool `help:"discard client logging"`
	Dht             bool

	TcpPeers        bool
	UtpPeers        bool
	Webtorrent      bool
	DisableWebseeds bool

	Ipv4 bool
	Ipv6 bool
	Pex  bool

	tagflag.StartPos

	Torrent []string `arity:"+" help:"torrent file path or magnet uri"`
}{
	UploadRate:   -1,
	DownloadRate: -1,
	Progress:     true,
	Dht:          true,

	TcpPeers:   true,
	UtpPeers:   true,
	Webtorrent: true,

	Ipv4: true,
	Ipv6: true,
	Pex:  true,
}

func stdoutAndStderrAreSameFile() bool {
	fi1, _ := os.Stdout.Stat()
	fi2, _ := os.Stderr.Stat()
	return os.SameFile(fi1, fi2)
}

func statsEnabled() bool {
	if flags.Stats == nil {
		return flags.Debug
	}
	return *flags.Stats
}

func exitSignalHandlers(notify *missinggo.SynchronizedEvent) {
	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
	for {
		log.Printf("close signal received: %+v", <-c)
		notify.Set()
	}
}

func main() {
	if err := mainErr(); err != nil {
		log.Printf("error in main: %v", err)
		os.Exit(1)
	}
}

func mainErr() error {
	var flags struct {
		tagflag.StartPos
		Command string
		Args    tagflag.ExcessArgs
	}
	parser := tagflag.Parse(&flags, tagflag.ParseIntermixed(false))
	switch flags.Command {
	case "announce":
		return announceErr(flags.Args, parser)
	case "download":
		return downloadErr(flags.Args, parser)
	default:
		return fmt.Errorf("unknown command %q", flags.Command)
	}
}

func downloadErr(args []string, parent *tagflag.Parser) error {
	tagflag.ParseArgs(&flags, args, tagflag.Parent(parent))
	defer envpprof.Stop()
	clientConfig := torrent.NewDefaultClientConfig()
	clientConfig.DisableWebseeds = flags.DisableWebseeds
	clientConfig.DisableTCP = !flags.TcpPeers
	clientConfig.DisableUTP = !flags.UtpPeers
	clientConfig.DisableIPv4 = !flags.Ipv4
	clientConfig.DisableIPv6 = !flags.Ipv6
	clientConfig.DisableAcceptRateLimiting = true
	clientConfig.NoDHT = !flags.Dht
	clientConfig.Debug = flags.Debug
	clientConfig.Seed = flags.Seed
	clientConfig.PublicIp4 = flags.PublicIP
	clientConfig.PublicIp6 = flags.PublicIP
	clientConfig.DisablePEX = !flags.Pex
	clientConfig.DisableWebtorrent = !flags.Webtorrent
	if flags.PackedBlocklist != "" {
		blocklist, err := iplist.MMapPackedFile(flags.PackedBlocklist)
		if err != nil {
			return xerrors.Errorf("loading blocklist: %v", err)
		}
		defer blocklist.Close()
		clientConfig.IPBlocklist = blocklist
	}
	if flags.Mmap {
		clientConfig.DefaultStorage = storage.NewMMap("")
	}
	if flags.Addr != "" {
		clientConfig.SetListenAddr(flags.Addr)
	}
	if flags.UploadRate != -1 {
		clientConfig.UploadRateLimiter = rate.NewLimiter(rate.Limit(flags.UploadRate), 256<<10)
	}
	if flags.DownloadRate != -1 {
		clientConfig.DownloadRateLimiter = rate.NewLimiter(rate.Limit(flags.DownloadRate), 1<<20)
	}
	if flags.Quiet {
		clientConfig.Logger = log.Discard
	}

	var stop missinggo.SynchronizedEvent
	defer func() {
		stop.Set()
	}()

	client, err := torrent.NewClient(clientConfig)
	if err != nil {
		return xerrors.Errorf("creating client: %v", err)
	}
	defer client.Close()
	go exitSignalHandlers(&stop)
	go func() {
		<-stop.C()
		client.Close()
	}()

	// Write status on the root path on the default HTTP muxer. This will be bound to localhost
	// somewhere if GOPPROF is set, thanks to the envpprof import.
	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		client.WriteStatus(w)
	})
	addTorrents(client)
	defer outputStats(client)
	if client.WaitAll() {
		log.Print("downloaded ALL the torrents")
	} else {
		return xerrors.New("y u no complete torrents?!")
	}
	if flags.Seed {
		outputStats(client)
		<-stop.C()
	}
	return nil
}

func outputStats(cl *torrent.Client) {
	if !statsEnabled() {
		return
	}
	expvar.Do(func(kv expvar.KeyValue) {
		fmt.Printf("%s: %s\n", kv.Key, kv.Value)
	})
	cl.WriteStatus(os.Stdout)
}
<commit_msg>Support -testPeer addresses that need resolving<commit_after>// Downloads torrents from the command-line.
package main

import (
	"expvar"
	"fmt"
	"net"
	"net/http"
	"os"
	"os/signal"
	"strings"
	"syscall"
	"time"

	"github.com/anacrolix/missinggo"
	"github.com/dustin/go-humanize"
	"golang.org/x/xerrors"

	"github.com/anacrolix/log"

	"github.com/anacrolix/envpprof"
	"github.com/anacrolix/tagflag"
	"golang.org/x/time/rate"

	"github.com/anacrolix/torrent"
	"github.com/anacrolix/torrent/iplist"
	"github.com/anacrolix/torrent/metainfo"
	"github.com/anacrolix/torrent/storage"
)

func torrentBar(t *torrent.Torrent, pieceStates bool) {
	go func() {
		if t.Info() == nil {
			fmt.Printf("getting info for %q\n", t.Name())
			<-t.GotInfo()
		}
		var lastLine string
		for {
			var completedPieces, partialPieces int
			psrs := t.PieceStateRuns()
			for _, r := range psrs {
				if r.Complete {
					completedPieces += r.Length
				}
				if r.Partial {
					partialPieces += r.Length
				}
			}
			line := fmt.Sprintf(
				"downloading %q: %s/%s, %d/%d pieces completed (%d partial)\n",
				t.Name(),
				humanize.Bytes(uint64(t.BytesCompleted())),
				humanize.Bytes(uint64(t.Length())),
				completedPieces,
				t.NumPieces(),
				partialPieces,
			)
			if line != lastLine {
				lastLine = line
				os.Stdout.WriteString(line)
			}
			if pieceStates {
				fmt.Println(psrs)
			}
			time.Sleep(time.Second)
		}
	}()
}

type stringAddr string

func (stringAddr) Network() string   { return "" }
func (me stringAddr) String() string { return string(me) }

func resolveTestPeers(addrs []string) (ret []torrent.PeerInfo) {
	for _, ta := range flags.TestPeer {
		ret = append(ret, torrent.PeerInfo{
			Addr: stringAddr(ta),
		})
	}
	return
}

func addTorrents(client *torrent.Client) error {
	testPeers := resolveTestPeers(flags.TestPeer)
	for _, arg := range flags.Torrent {
		t, err := func() (*torrent.Torrent, error) {
			if strings.HasPrefix(arg, "magnet:") {
				t, err := client.AddMagnet(arg)
				if err != nil {
					return nil, xerrors.Errorf("error adding magnet: %w", err)
				}
				return t, nil
			} else if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
				response, err := http.Get(arg)
				if err != nil {
					return nil, xerrors.Errorf("Error downloading torrent file: %s", err)
				}

				metaInfo, err := metainfo.Load(response.Body)
				defer response.Body.Close()
				if err != nil {
					return nil, xerrors.Errorf("error loading torrent file %q: %s\n", arg, err)
				}
				t, err := client.AddTorrent(metaInfo)
				if err != nil {
					return nil, xerrors.Errorf("adding torrent: %w", err)
				}
				return t, nil
			} else if strings.HasPrefix(arg, "infohash:") {
				t, _ := client.AddTorrentInfoHash(metainfo.NewHashFromHex(strings.TrimPrefix(arg, "infohash:")))
				return t, nil
			} else {
				metaInfo, err := metainfo.LoadFromFile(arg)
				if err != nil {
					return nil, xerrors.Errorf("error loading torrent file %q: %s\n", arg, err)
				}
				t, err := client.AddTorrent(metaInfo)
				if err != nil {
					return nil, xerrors.Errorf("adding torrent: %w", err)
				}
				return t, nil
			}
		}()
		if err != nil {
			return xerrors.Errorf("adding torrent for %q: %w", arg, err)
		}
		if flags.Progress {
			torrentBar(t, flags.PieceStates)
		}
		t.AddPeers(testPeers)
		go func() {
			<-t.GotInfo()
			t.DownloadAll()
		}()
	}
	return nil
}

var flags = struct {
	Mmap            bool          `help:"memory-map torrent data"`
	TestPeer        []string      `help:"addresses of some starting peers"`
	Seed            bool          `help:"seed after download is complete"`
	Addr            string        `help:"network listen addr"`
	UploadRate      tagflag.Bytes `help:"max piece bytes to send per second"`
	DownloadRate    tagflag.Bytes `help:"max bytes per second down from peers"`
	Debug           bool
	PackedBlocklist string
	Stats           *bool
	PublicIP        net.IP
	Progress        bool
	PieceStates     bool
	Quiet           bool `help:"discard client logging"`
	Dht             bool

	TcpPeers        bool
	UtpPeers        bool
	Webtorrent      bool
	DisableWebseeds bool

	Ipv4 bool
	Ipv6 bool
	Pex  bool

	tagflag.StartPos

	Torrent []string `arity:"+" help:"torrent file path or magnet uri"`
}{
	UploadRate:   -1,
	DownloadRate: -1,
	Progress:     true,
	Dht:          true,

	TcpPeers:   true,
	UtpPeers:   true,
	Webtorrent: true,

	Ipv4: true,
	Ipv6: true,
	Pex:  true,
}

func stdoutAndStderrAreSameFile() bool {
	fi1, _ := os.Stdout.Stat()
	fi2, _ := os.Stderr.Stat()
	return os.SameFile(fi1, fi2)
}

func statsEnabled() bool {
	if flags.Stats == nil {
		return flags.Debug
	}
	return *flags.Stats
}

func exitSignalHandlers(notify *missinggo.SynchronizedEvent) {
	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
	for {
		log.Printf("close signal received: %+v", <-c)
		notify.Set()
	}
}

func main() {
	if err := mainErr(); err != nil {
		log.Printf("error in main: %v", err)
		os.Exit(1)
	}
}

func mainErr() error {
	var flags struct {
		tagflag.StartPos
		Command string
		Args    tagflag.ExcessArgs
	}
	parser := tagflag.Parse(&flags, tagflag.ParseIntermixed(false))
	switch flags.Command {
	case "announce":
		return announceErr(flags.Args, parser)
	case "download":
		return downloadErr(flags.Args, parser)
	default:
		return fmt.Errorf("unknown command %q", flags.Command)
	}
}

func downloadErr(args []string, parent *tagflag.Parser) error {
	tagflag.ParseArgs(&flags, args, tagflag.Parent(parent))
	defer envpprof.Stop()
	clientConfig := torrent.NewDefaultClientConfig()
	clientConfig.DisableWebseeds = flags.DisableWebseeds
	clientConfig.DisableTCP = !flags.TcpPeers
	clientConfig.DisableUTP = !flags.UtpPeers
	clientConfig.DisableIPv4 = !flags.Ipv4
	clientConfig.DisableIPv6 = !flags.Ipv6
	clientConfig.DisableAcceptRateLimiting = true
	clientConfig.NoDHT = !flags.Dht
	clientConfig.Debug = flags.Debug
	clientConfig.Seed = flags.Seed
	clientConfig.PublicIp4 = flags.PublicIP
	clientConfig.PublicIp6 = flags.PublicIP
	clientConfig.DisablePEX = !flags.Pex
	clientConfig.DisableWebtorrent = !flags.Webtorrent
	if flags.PackedBlocklist != "" {
		blocklist, err := iplist.MMapPackedFile(flags.PackedBlocklist)
		if err != nil {
			return xerrors.Errorf("loading blocklist: %v", err)
		}
		defer blocklist.Close()
		clientConfig.IPBlocklist = blocklist
	}
	if flags.Mmap {
		clientConfig.DefaultStorage = storage.NewMMap("")
	}
	if flags.Addr != "" {
		clientConfig.SetListenAddr(flags.Addr)
	}
	if flags.UploadRate != -1 {
		clientConfig.UploadRateLimiter = rate.NewLimiter(rate.Limit(flags.UploadRate), 256<<10)
	}
	if flags.DownloadRate != -1 {
		clientConfig.DownloadRateLimiter = rate.NewLimiter(rate.Limit(flags.DownloadRate), 1<<20)
	}
	if flags.Quiet {
		clientConfig.Logger = log.Discard
	}

	var stop missinggo.SynchronizedEvent
	defer func() {
		stop.Set()
	}()

	client, err := torrent.NewClient(clientConfig)
	if err != nil {
		return xerrors.Errorf("creating client: %v", err)
	}
	defer client.Close()
	go exitSignalHandlers(&stop)
	go func() {
		<-stop.C()
		client.Close()
	}()

	// Write status on the root path on the default HTTP muxer. This will be bound to localhost
	// somewhere if GOPPROF is set, thanks to the envpprof import.
	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		client.WriteStatus(w)
	})
	addTorrents(client)
	defer outputStats(client)
	if client.WaitAll() {
		log.Print("downloaded ALL the torrents")
	} else {
		return xerrors.New("y u no complete torrents?!")
	}
	if flags.Seed {
		outputStats(client)
		<-stop.C()
	}
	return nil
}

func outputStats(cl *torrent.Client) {
	if !statsEnabled() {
		return
	}
	expvar.Do(func(kv expvar.KeyValue) {
		fmt.Printf("%s: %s\n", kv.Key, kv.Value)
	})
	cl.WriteStatus(os.Stdout)
}
<|endoftext|>


<commit_before>package goldmark

import (
	"github.com/yuin/goldmark/parser"
	"testing"
)

func TestDefinitionList(t *testing.T) {
	markdown := New(
		WithParserOptions(
			parser.WithAttribute(),
			parser.WithAutoHeadingID(),
		),
	)
	DoTestCaseFile(markdown, "_test/options.txt", t)
}
<commit_msg>Rename test function<commit_after>package goldmark

import (
	"github.com/yuin/goldmark/parser"
	"testing"
)

func TestAttributeAndAutoHeadingID(t *testing.T) {
	markdown := New(
		WithParserOptions(
			parser.WithAttribute(),
			parser.WithAutoHeadingID(),
		),
	)
	DoTestCaseFile(markdown, "_test/options.txt", t)
}
<|endoftext|>


<commit_before>package dhtopts

import (
	"fmt"
	"time"

	ds "github.com/ipfs/go-datastore"
	dssync "github.com/ipfs/go-datastore/sync"
	"github.com/libp2p/go-libp2p-core/protocol"
	"github.com/libp2p/go-libp2p-record"
)

// Deprecated: The old format did not support more than one message per stream, and is not supported
// or relevant with stream pooling. ProtocolDHT should be used instead.
const ProtocolDHTOld protocol.ID = "/ipfs/dht"

var (
	ProtocolDHT      protocol.ID = "/ipfs/kad/1.0.0"
	DefaultProtocols             = []protocol.ID{ProtocolDHT}
)

// Options is a structure containing all the options that can be used when constructing a DHT.
type Options struct {
	Datastore       ds.Batching
	Validator       record.Validator
	Client          bool
	Protocols       []protocol.ID
	BucketSize      int
	MaxRecordAge    time.Duration
	EnableProviders bool
	EnableValues    bool

	RoutingTable struct {
		RefreshQueryTimeout time.Duration
		RefreshPeriod       time.Duration
		AutoRefresh         bool
		LatencyTolerance    time.Duration
	}
}

// Apply applies the given options to this Option
func (o *Options) Apply(opts ...Option) error {
	for i, opt := range opts {
		if err := opt(o); err != nil {
			return fmt.Errorf("dht option %d failed: %s", i, err)
		}
	}
	return nil
}

// Option DHT option type.
type Option func(*Options) error

// Defaults are the default DHT options. This option will be automatically
// prepended to any options you pass to the DHT constructor.
var Defaults = func(o *Options) error {
	o.Validator = record.NamespacedValidator{
		"pk": record.PublicKeyValidator{},
	}
	o.Datastore = dssync.MutexWrap(ds.NewMapDatastore())
	o.Protocols = DefaultProtocols
	o.EnableProviders = true
	o.EnableValues = true

	o.RoutingTable.RefreshQueryTimeout = 10 * time.Second
	o.RoutingTable.RefreshPeriod = 1 * time.Hour
	o.RoutingTable.AutoRefresh = true
	o.MaxRecordAge = time.Hour * 36

	return nil
}

func RoutingTableLatencyTolerance(latency time.Duration) Option {
	return func(o *Options) error {
		o.RoutingTable.LatencyTolerance = latency
		return nil
	}
}

// RoutingTableRefreshQueryTimeout sets the timeout for routing table refresh
// queries.
func RoutingTableRefreshQueryTimeout(timeout time.Duration) Option {
	return func(o *Options) error {
		o.RoutingTable.RefreshQueryTimeout = timeout
		return nil
	}
}

// RoutingTableRefreshPeriod sets the period for refreshing buckets in the
// routing table. The DHT will refresh buckets every period by:
//
// 1. First searching for nearby peers to figure out how many buckets we should try to fill.
// 1. Then searching for a random key in each bucket that hasn't been queried in
//    the last refresh period.
func RoutingTableRefreshPeriod(period time.Duration) Option {
	return func(o *Options) error {
		o.RoutingTable.RefreshPeriod = period
		return nil
	}
}

// Datastore configures the DHT to use the specified datastore.
//
// Defaults to an in-memory (temporary) map.
func Datastore(ds ds.Batching) Option {
	return func(o *Options) error {
		o.Datastore = ds
		return nil
	}
}

// Client configures whether or not the DHT operates in client-only mode.
//
// Defaults to false.
func Client(only bool) Option {
	return func(o *Options) error {
		o.Client = only
		return nil
	}
}

// Validator configures the DHT to use the specified validator.
//
// Defaults to a namespaced validator that can only validate public keys.
func Validator(v record.Validator) Option {
	return func(o *Options) error {
		o.Validator = v
		return nil
	}
}

// NamespacedValidator adds a validator namespaced under `ns`. This option fails
// if the DHT is not using a `record.NamespacedValidator` as it's validator (it
// uses one by default but this can be overridden with the `Validator` option).
//
// Example: Given a validator registered as `NamespacedValidator("ipns",
// myValidator)`, all records with keys starting with `/ipns/` will be validated
// with `myValidator`.
func NamespacedValidator(ns string, v record.Validator) Option {
	return func(o *Options) error {
		nsval, ok := o.Validator.(record.NamespacedValidator)
		if !ok {
			return fmt.Errorf("can only add namespaced validators to a NamespacedValidator")
		}
		nsval[ns] = v
		return nil
	}
}

// Protocols sets the protocols for the DHT
//
// Defaults to dht.DefaultProtocols
func Protocols(protocols ...protocol.ID) Option {
	return func(o *Options) error {
		o.Protocols = protocols
		return nil
	}
}

// BucketSize configures the bucket size of the routing table.
//
// The default value is 20.
func BucketSize(bucketSize int) Option {
	return func(o *Options) error {
		o.BucketSize = bucketSize
		return nil
	}
}

// MaxRecordAge specifies the maximum time that any node will hold onto a record ("PutValue record")
// from the time its received. This does not apply to any other forms of validity that
// the record may contain.
// For example, a record may contain an ipns entry with an EOL saying its valid
// until the year 2020 (a great time in the future). For that record to stick around
// it must be rebroadcasted more frequently than once every 'MaxRecordAge'
func MaxRecordAge(maxAge time.Duration) Option {
	return func(o *Options) error {
		o.MaxRecordAge = maxAge
		return nil
	}
}

// DisableAutoRefresh completely disables 'auto-refresh' on the DHT routing
// table. This means that we will neither refresh the routing table periodically
// nor when the routing table size goes below the minimum threshold.
func DisableAutoRefresh() Option {
	return func(o *Options) error {
		o.RoutingTable.AutoRefresh = false
		return nil
	}
}

// DisableProviders disables storing and retrieving provider records.
//
// Defaults to enabled.
//
// WARNING: do not change this unless you're using a forked DHT (i.e., a private
// network and/or distinct DHT protocols with the `Protocols` option).
func DisableProviders() Option {
	return func(o *Options) error {
		o.EnableProviders = false
		return nil
	}
}

// DisableProviders disables storing and retrieving value records (including
// public keys).
//
// Defaults to enabled.
//
// WARNING: do not change this unless you're using a forked DHT (i.e., a private
// network and/or distinct DHT protocols with the `Protocols` option).
func DisableValues() Option {
	return func(o *Options) error {
		o.EnableValues = false
		return nil
	}
}
<commit_msg>Set defaults for RoutingTable.LatencyTolerance.<commit_after>package dhtopts

import (
	"fmt"
	"time"

	ds "github.com/ipfs/go-datastore"
	dssync "github.com/ipfs/go-datastore/sync"
	"github.com/libp2p/go-libp2p-core/protocol"
	"github.com/libp2p/go-libp2p-record"
)

// Deprecated: The old format did not support more than one message per stream, and is not supported
// or relevant with stream pooling. ProtocolDHT should be used instead.
const ProtocolDHTOld protocol.ID = "/ipfs/dht"

var (
	ProtocolDHT      protocol.ID = "/ipfs/kad/1.0.0"
	DefaultProtocols             = []protocol.ID{ProtocolDHT}
)

// Options is a structure containing all the options that can be used when constructing a DHT.
type Options struct {
	Datastore       ds.Batching
	Validator       record.Validator
	Client          bool
	Protocols       []protocol.ID
	BucketSize      int
	MaxRecordAge    time.Duration
	EnableProviders bool
	EnableValues    bool

	RoutingTable struct {
		RefreshQueryTimeout time.Duration
		RefreshPeriod       time.Duration
		AutoRefresh         bool
		LatencyTolerance    time.Duration
	}
}

// Apply applies the given options to this Option
func (o *Options) Apply(opts ...Option) error {
	for i, opt := range opts {
		if err := opt(o); err != nil {
			return fmt.Errorf("dht option %d failed: %s", i, err)
		}
	}
	return nil
}

// Option DHT option type.
type Option func(*Options) error

// Defaults are the default DHT options. This option will be automatically
// prepended to any options you pass to the DHT constructor.
var Defaults = func(o *Options) error {
	o.Validator = record.NamespacedValidator{
		"pk": record.PublicKeyValidator{},
	}
	o.Datastore = dssync.MutexWrap(ds.NewMapDatastore())
	o.Protocols = DefaultProtocols
	o.EnableProviders = true
	o.EnableValues = true

	o.RoutingTable.LatencyTolerance = time.Minute
	o.RoutingTable.RefreshQueryTimeout = 10 * time.Second
	o.RoutingTable.RefreshPeriod = 1 * time.Hour
	o.RoutingTable.AutoRefresh = true
	o.MaxRecordAge = time.Hour * 36

	return nil
}

func RoutingTableLatencyTolerance(latency time.Duration) Option {
	return func(o *Options) error {
		o.RoutingTable.LatencyTolerance = latency
		return nil
	}
}

// RoutingTableRefreshQueryTimeout sets the timeout for routing table refresh
// queries.
func RoutingTableRefreshQueryTimeout(timeout time.Duration) Option {
	return func(o *Options) error {
		o.RoutingTable.RefreshQueryTimeout = timeout
		return nil
	}
}

// RoutingTableRefreshPeriod sets the period for refreshing buckets in the
// routing table. The DHT will refresh buckets every period by:
//
// 1. First searching for nearby peers to figure out how many buckets we should try to fill.
// 1. Then searching for a random key in each bucket that hasn't been queried in
//    the last refresh period.
func RoutingTableRefreshPeriod(period time.Duration) Option {
	return func(o *Options) error {
		o.RoutingTable.RefreshPeriod = period
		return nil
	}
}

// Datastore configures the DHT to use the specified datastore.
//
// Defaults to an in-memory (temporary) map.
func Datastore(ds ds.Batching) Option {
	return func(o *Options) error {
		o.Datastore = ds
		return nil
	}
}

// Client configures whether or not the DHT operates in client-only mode.
//
// Defaults to false.
func Client(only bool) Option {
	return func(o *Options) error {
		o.Client = only
		return nil
	}
}

// Validator configures the DHT to use the specified validator.
//
// Defaults to a namespaced validator that can only validate public keys.
func Validator(v record.Validator) Option {
	return func(o *Options) error {
		o.Validator = v
		return nil
	}
}

// NamespacedValidator adds a validator namespaced under `ns`. This option fails
// if the DHT is not using a `record.NamespacedValidator` as it's validator (it
// uses one by default but this can be overridden with the `Validator` option).
//
// Example: Given a validator registered as `NamespacedValidator("ipns",
// myValidator)`, all records with keys starting with `/ipns/` will be validated
// with `myValidator`.
func NamespacedValidator(ns string, v record.Validator) Option {
	return func(o *Options) error {
		nsval, ok := o.Validator.(record.NamespacedValidator)
		if !ok {
			return fmt.Errorf("can only add namespaced validators to a NamespacedValidator")
		}
		nsval[ns] = v
		return nil
	}
}

// Protocols sets the protocols for the DHT
//
// Defaults to dht.DefaultProtocols
func Protocols(protocols ...protocol.ID) Option {
	return func(o *Options) error {
		o.Protocols = protocols
		return nil
	}
}

// BucketSize configures the bucket size of the routing table.
//
// The default value is 20.
func BucketSize(bucketSize int) Option {
	return func(o *Options) error {
		o.BucketSize = bucketSize
		return nil
	}
}

// MaxRecordAge specifies the maximum time that any node will hold onto a record ("PutValue record")
// from the time its received. This does not apply to any other forms of validity that
// the record may contain.
// For example, a record may contain an ipns entry with an EOL saying its valid
// until the year 2020 (a great time in the future). For that record to stick around
// it must be rebroadcasted more frequently than once every 'MaxRecordAge'
func MaxRecordAge(maxAge time.Duration) Option {
	return func(o *Options) error {
		o.MaxRecordAge = maxAge
		return nil
	}
}

// DisableAutoRefresh completely disables 'auto-refresh' on the DHT routing
// table. This means that we will neither refresh the routing table periodically
// nor when the routing table size goes below the minimum threshold.
func DisableAutoRefresh() Option {
	return func(o *Options) error {
		o.RoutingTable.AutoRefresh = false
		return nil
	}
}

// DisableProviders disables storing and retrieving provider records.
//
// Defaults to enabled.
//
// WARNING: do not change this unless you're using a forked DHT (i.e., a private
// network and/or distinct DHT protocols with the `Protocols` option).
func DisableProviders() Option {
	return func(o *Options) error {
		o.EnableProviders = false
		return nil
	}
}

// DisableProviders disables storing and retrieving value records (including
// public keys).
//
// Defaults to enabled.
//
// WARNING: do not change this unless you're using a forked DHT (i.e., a private
// network and/or distinct DHT protocols with the `Protocols` option).
func DisableValues() Option {
	return func(o *Options) error {
		o.EnableValues = false
		return nil
	}
}
<|endoftext|>