<commit_before>package main

import (
	"fmt"
	"os"
	"os/exec"
	"runtime"
	"runtime/pprof"
	"strings"
	"syscall"

	"github.com/containers/buildah"
	"github.com/containers/buildah/define"
	"github.com/containers/buildah/pkg/cli"
	"github.com/containers/buildah/pkg/parse"
	"github.com/containers/common/pkg/config"
	"github.com/containers/storage"
	"github.com/containers/storage/pkg/unshare"
	ispecs "github.com/opencontainers/image-spec/specs-go"
	rspecs "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
)

type globalFlags struct {
	Debug             bool
	LogLevel          string
	Root              string
	RunRoot           string
	StorageDriver     string
	RegistriesConf    string
	RegistriesConfDir string
	DefaultMountsFile string
	StorageOpts       []string
	UserNSUID         []string
	UserNSGID         []string
	CPUProfile        string
	cpuProfileFile    *os.File
	MemoryProfile     string
}

var rootCmd = &cobra.Command{
	Use:  "buildah",
	Long: "A tool that facilitates building OCI images",
	RunE: func(cmd *cobra.Command, args []string) error {
		return cmd.Help()
	},
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		return before(cmd)
	},
	PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
		return after(cmd)
	},
	SilenceUsage:  true,
	SilenceErrors: true,
}

var (
	globalFlagResults globalFlags
)

func init() {
	logrus.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true})

	var (
		defaultStoreDriverOptions []string
	)
	storageOptions, err := storage.DefaultStoreOptions(false, 0)
	if err != nil {
		logrus.Errorf(err.Error())
		os.Exit(1)

	}

	if len(storageOptions.GraphDriverOptions) > 0 {
		optionSlice := storageOptions.GraphDriverOptions[:]
		defaultStoreDriverOptions = optionSlice
	}

	cobra.OnInitialize(initConfig)
	//rootCmd.TraverseChildren = true
	rootCmd.Version = fmt.Sprintf("%s (image-spec %s, runtime-spec %s)", define.Version, ispecs.Version, rspecs.Version)
	rootCmd.PersistentFlags().BoolVar(&globalFlagResults.Debug, "debug", false, "print debugging information")
	// TODO Need to allow for environment variable
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.RegistriesConf, "registries-conf", "", "path to registries.conf file (not usually used)")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.RegistriesConfDir, "registries-conf-dir", "", "path to registries.conf.d directory (not usually used)")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.Root, "root", storageOptions.GraphRoot, "storage root dir")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.RunRoot, "runroot", storageOptions.RunRoot, "storage state dir")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.StorageDriver, "storage-driver", storageOptions.GraphDriverName, "storage-driver")
	rootCmd.PersistentFlags().StringSliceVar(&globalFlagResults.StorageOpts, "storage-opt", defaultStoreDriverOptions, "storage driver option")
	rootCmd.PersistentFlags().StringSliceVar(&globalFlagResults.UserNSUID, "userns-uid-map", []string{}, "default `ctrID:hostID:length` UID mapping to use")
	rootCmd.PersistentFlags().StringSliceVar(&globalFlagResults.UserNSGID, "userns-gid-map", []string{}, "default `ctrID:hostID:length` GID mapping to use")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.DefaultMountsFile, "default-mounts-file", "", "path to default mounts file")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.LogLevel, logLevel, "warn", `The log level to be used. Either "debug", "info", "warn" or "error".`)
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.CPUProfile, "cpu-profile", "", "`file` to write CPU profile")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.MemoryProfile, "memory-profile", "", "`file` to write memory profile")

	if err := rootCmd.PersistentFlags().MarkHidden("cpu-profile"); err != nil {
		logrus.Fatalf("unable to mark cpu-profile flag as hidden: %v", err)
	}
	if err := rootCmd.PersistentFlags().MarkHidden("debug"); err != nil {
		logrus.Fatalf("unable to mark debug flag as hidden: %v", err)
	}
	if err := rootCmd.PersistentFlags().MarkHidden("default-mounts-file"); err != nil {
		logrus.Fatalf("unable to mark default-mounts-file flag as hidden: %v", err)
	}
	if err := rootCmd.PersistentFlags().MarkHidden("memory-profile"); err != nil {
		logrus.Fatalf("unable to mark memory-profile flag as hidden: %v", err)
	}
}

func initConfig() {
	// TODO Cobra allows us to do extra stuff here at init
	// time if we ever want to take advantage.
}

const logLevel = "log-level"

func before(cmd *cobra.Command) error {
	strLvl, err := cmd.Flags().GetString(logLevel)
	if err != nil {
		return err
	}
	logrusLvl, err := logrus.ParseLevel(strLvl)
	if err != nil {
		return errors.Wrapf(err, "unable to parse log level")
	}
	logrus.SetLevel(logrusLvl)
	if globalFlagResults.Debug {
		logrus.SetLevel(logrus.DebugLevel)
	}

	switch cmd.Use {
	case "", "help", "version", "mount":
		return nil
	}
	unshare.MaybeReexecUsingUserNamespace(false)
	if globalFlagResults.CPUProfile != "" {
		globalFlagResults.cpuProfileFile, err = os.Create(globalFlagResults.CPUProfile)
		if err != nil {
			logrus.Fatalf("could not create CPU profile %s: %v", globalFlagResults.CPUProfile, err)
		}
		if err = pprof.StartCPUProfile(globalFlagResults.cpuProfileFile); err != nil {
			logrus.Fatalf("error starting CPU profiling: %v", err)
		}
	}

	defaultContainerConfig, err := config.Default()
	if err != nil {
		return err
	}

	for _, env := range defaultContainerConfig.Engine.Env {
		splitEnv := strings.SplitN(env, "=", 2)
		if len(splitEnv) != 2 {
			return fmt.Errorf("invalid environment variable %q from containers.conf, valid configuration is KEY=value pair", env)
		}
		// skip if the env is already defined
		if _, ok := os.LookupEnv(splitEnv[0]); ok {
			logrus.Debugf("environment variable %q is already defined, skip the settings from containers.conf", splitEnv[0])
			continue
		}
		if err := os.Setenv(splitEnv[0], splitEnv[1]); err != nil {
			return err
		}
	}

	return nil
}

func shutdownStore(cmd *cobra.Command) error {
	if needToShutdownStore {
		store, err := getStore(cmd)
		if err != nil {
			return err
		}
		logrus.Debugf("shutting down the store")
		needToShutdownStore = false
		if _, err = store.Shutdown(false); err != nil {
			if errors.Cause(err) == storage.ErrLayerUsedByContainer {
				logrus.Infof("failed to shutdown storage: %q", err)
			} else {
				logrus.Warnf("failed to shutdown storage: %q", err)
			}
		}
	}
	return nil
}

func after(cmd *cobra.Command) error {
	if err := shutdownStore(cmd); err != nil {
		return err
	}

	if globalFlagResults.CPUProfile != "" {
		pprof.StopCPUProfile()
		globalFlagResults.cpuProfileFile.Close()
	}
	if globalFlagResults.MemoryProfile != "" {
		memoryProfileFile, err := os.Create(globalFlagResults.MemoryProfile)
		if err != nil {
			logrus.Fatalf("could not create memory profile %s: %v", globalFlagResults.MemoryProfile, err)
		}
		defer memoryProfileFile.Close()
		runtime.GC()
		if err := pprof.Lookup("heap").WriteTo(memoryProfileFile, 1); err != nil {
			logrus.Fatalf("could not write memory profile %s: %v", globalFlagResults.MemoryProfile, err)
		}
	}
	return nil
}

func main() {
	if buildah.InitReexec() {
		return
	}

	// Hard code TMPDIR functions to use $TMPDIR or /var/tmp
	os.Setenv("TMPDIR", parse.GetTempDir())

	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		exitCode := cli.ExecErrorCodeGeneric
		if ee, ok := (errors.Cause(err)).(*exec.ExitError); ok {
			if w, ok := ee.Sys().(syscall.WaitStatus); ok {
				exitCode = w.ExitStatus()
			}
		}
		if err := shutdownStore(rootCmd); err != nil {
			logrus.Warnf("failed to shutdown storage: %q", err)
		}
		os.Exit(exitCode)
	}
}
<commit_msg>buildah login and logout, do not need to enter user namespace<commit_after>package main

import (
	"fmt"
	"os"
	"os/exec"
	"runtime"
	"runtime/pprof"
	"strings"
	"syscall"

	"github.com/containers/buildah"
	"github.com/containers/buildah/define"
	"github.com/containers/buildah/pkg/cli"
	"github.com/containers/buildah/pkg/parse"
	"github.com/containers/common/pkg/config"
	"github.com/containers/storage"
	"github.com/containers/storage/pkg/unshare"
	ispecs "github.com/opencontainers/image-spec/specs-go"
	rspecs "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
)

type globalFlags struct {
	Debug             bool
	LogLevel          string
	Root              string
	RunRoot           string
	StorageDriver     string
	RegistriesConf    string
	RegistriesConfDir string
	DefaultMountsFile string
	StorageOpts       []string
	UserNSUID         []string
	UserNSGID         []string
	CPUProfile        string
	cpuProfileFile    *os.File
	MemoryProfile     string
}

var rootCmd = &cobra.Command{
	Use:  "buildah",
	Long: "A tool that facilitates building OCI images",
	RunE: func(cmd *cobra.Command, args []string) error {
		return cmd.Help()
	},
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		return before(cmd)
	},
	PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
		return after(cmd)
	},
	SilenceUsage:  true,
	SilenceErrors: true,
}

var (
	globalFlagResults globalFlags
)

func init() {
	logrus.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true})

	var (
		defaultStoreDriverOptions []string
	)
	storageOptions, err := storage.DefaultStoreOptions(false, 0)
	if err != nil {
		logrus.Errorf(err.Error())
		os.Exit(1)

	}

	if len(storageOptions.GraphDriverOptions) > 0 {
		optionSlice := storageOptions.GraphDriverOptions[:]
		defaultStoreDriverOptions = optionSlice
	}

	cobra.OnInitialize(initConfig)
	//rootCmd.TraverseChildren = true
	rootCmd.Version = fmt.Sprintf("%s (image-spec %s, runtime-spec %s)", define.Version, ispecs.Version, rspecs.Version)
	rootCmd.PersistentFlags().BoolVar(&globalFlagResults.Debug, "debug", false, "print debugging information")
	// TODO Need to allow for environment variable
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.RegistriesConf, "registries-conf", "", "path to registries.conf file (not usually used)")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.RegistriesConfDir, "registries-conf-dir", "", "path to registries.conf.d directory (not usually used)")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.Root, "root", storageOptions.GraphRoot, "storage root dir")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.RunRoot, "runroot", storageOptions.RunRoot, "storage state dir")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.StorageDriver, "storage-driver", storageOptions.GraphDriverName, "storage-driver")
	rootCmd.PersistentFlags().StringSliceVar(&globalFlagResults.StorageOpts, "storage-opt", defaultStoreDriverOptions, "storage driver option")
	rootCmd.PersistentFlags().StringSliceVar(&globalFlagResults.UserNSUID, "userns-uid-map", []string{}, "default `ctrID:hostID:length` UID mapping to use")
	rootCmd.PersistentFlags().StringSliceVar(&globalFlagResults.UserNSGID, "userns-gid-map", []string{}, "default `ctrID:hostID:length` GID mapping to use")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.DefaultMountsFile, "default-mounts-file", "", "path to default mounts file")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.LogLevel, logLevel, "warn", `The log level to be used. Either "debug", "info", "warn" or "error".`)
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.CPUProfile, "cpu-profile", "", "`file` to write CPU profile")
	rootCmd.PersistentFlags().StringVar(&globalFlagResults.MemoryProfile, "memory-profile", "", "`file` to write memory profile")

	if err := rootCmd.PersistentFlags().MarkHidden("cpu-profile"); err != nil {
		logrus.Fatalf("unable to mark cpu-profile flag as hidden: %v", err)
	}
	if err := rootCmd.PersistentFlags().MarkHidden("debug"); err != nil {
		logrus.Fatalf("unable to mark debug flag as hidden: %v", err)
	}
	if err := rootCmd.PersistentFlags().MarkHidden("default-mounts-file"); err != nil {
		logrus.Fatalf("unable to mark default-mounts-file flag as hidden: %v", err)
	}
	if err := rootCmd.PersistentFlags().MarkHidden("memory-profile"); err != nil {
		logrus.Fatalf("unable to mark memory-profile flag as hidden: %v", err)
	}
}

func initConfig() {
	// TODO Cobra allows us to do extra stuff here at init
	// time if we ever want to take advantage.
}

const logLevel = "log-level"

func before(cmd *cobra.Command) error {
	strLvl, err := cmd.Flags().GetString(logLevel)
	if err != nil {
		return err
	}
	logrusLvl, err := logrus.ParseLevel(strLvl)
	if err != nil {
		return errors.Wrapf(err, "unable to parse log level")
	}
	logrus.SetLevel(logrusLvl)
	if globalFlagResults.Debug {
		logrus.SetLevel(logrus.DebugLevel)
	}

	switch cmd.Use {
	case "", "help", "version", "mount", "login", "logout":
		return nil
	}
	unshare.MaybeReexecUsingUserNamespace(false)
	if globalFlagResults.CPUProfile != "" {
		globalFlagResults.cpuProfileFile, err = os.Create(globalFlagResults.CPUProfile)
		if err != nil {
			logrus.Fatalf("could not create CPU profile %s: %v", globalFlagResults.CPUProfile, err)
		}
		if err = pprof.StartCPUProfile(globalFlagResults.cpuProfileFile); err != nil {
			logrus.Fatalf("error starting CPU profiling: %v", err)
		}
	}

	defaultContainerConfig, err := config.Default()
	if err != nil {
		return err
	}

	for _, env := range defaultContainerConfig.Engine.Env {
		splitEnv := strings.SplitN(env, "=", 2)
		if len(splitEnv) != 2 {
			return fmt.Errorf("invalid environment variable %q from containers.conf, valid configuration is KEY=value pair", env)
		}
		// skip if the env is already defined
		if _, ok := os.LookupEnv(splitEnv[0]); ok {
			logrus.Debugf("environment variable %q is already defined, skip the settings from containers.conf", splitEnv[0])
			continue
		}
		if err := os.Setenv(splitEnv[0], splitEnv[1]); err != nil {
			return err
		}
	}

	return nil
}

func shutdownStore(cmd *cobra.Command) error {
	if needToShutdownStore {
		store, err := getStore(cmd)
		if err != nil {
			return err
		}
		logrus.Debugf("shutting down the store")
		needToShutdownStore = false
		if _, err = store.Shutdown(false); err != nil {
			if errors.Cause(err) == storage.ErrLayerUsedByContainer {
				logrus.Infof("failed to shutdown storage: %q", err)
			} else {
				logrus.Warnf("failed to shutdown storage: %q", err)
			}
		}
	}
	return nil
}

func after(cmd *cobra.Command) error {
	if err := shutdownStore(cmd); err != nil {
		return err
	}

	if globalFlagResults.CPUProfile != "" {
		pprof.StopCPUProfile()
		globalFlagResults.cpuProfileFile.Close()
	}
	if globalFlagResults.MemoryProfile != "" {
		memoryProfileFile, err := os.Create(globalFlagResults.MemoryProfile)
		if err != nil {
			logrus.Fatalf("could not create memory profile %s: %v", globalFlagResults.MemoryProfile, err)
		}
		defer memoryProfileFile.Close()
		runtime.GC()
		if err := pprof.Lookup("heap").WriteTo(memoryProfileFile, 1); err != nil {
			logrus.Fatalf("could not write memory profile %s: %v", globalFlagResults.MemoryProfile, err)
		}
	}
	return nil
}

func main() {
	if buildah.InitReexec() {
		return
	}

	// Hard code TMPDIR functions to use $TMPDIR or /var/tmp
	os.Setenv("TMPDIR", parse.GetTempDir())

	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		exitCode := cli.ExecErrorCodeGeneric
		if ee, ok := (errors.Cause(err)).(*exec.ExitError); ok {
			if w, ok := ee.Sys().(syscall.WaitStatus); ok {
				exitCode = w.ExitStatus()
			}
		}
		if err := shutdownStore(rootCmd); err != nil {
			logrus.Warnf("failed to shutdown storage: %q", err)
		}
		os.Exit(exitCode)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"os"

	"github.com/src-d/gitbase/cmd/gitbase/command"

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

const (
	name    = "gitbase"
	version = "undefined"
	build   = "undefined"
)

func main() {
	parser := flags.NewNamedParser(name, flags.Default)

	_, err := parser.AddCommand("server", command.ServerDescription, command.ServerHelp,
		&command.Server{
			SkipGitErrors: os.Getenv("GITBASE_SKIP_GIT_ERRORS") != "",
			Version:       version,
		})
	if err != nil {
		logrus.Fatal(err)
	}

	_, err = parser.AddCommand("version", command.VersionDescription, command.VersionHelp,
		&command.Version{
			Name:    name,
			Version: version,
			Build:   build,
		})
	if err != nil {
		logrus.Fatal(err)
	}

	_, err = parser.Parse()
	if err != nil {
		if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrCommandRequired {
			parser.WriteHelp(os.Stdout)
		}

		os.Exit(1)
	}
}
<commit_msg>Keep version, passed at compile time<commit_after>package main

import (
	"os"

	"github.com/src-d/gitbase/cmd/gitbase/command"

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

var (
	name    = "gitbase"
	version = "undefined"
	build   = "undefined"
)

func main() {
	parser := flags.NewNamedParser(name, flags.Default)

	_, err := parser.AddCommand("server", command.ServerDescription, command.ServerHelp,
		&command.Server{
			SkipGitErrors: os.Getenv("GITBASE_SKIP_GIT_ERRORS") != "",
			Version:       version,
		})
	if err != nil {
		logrus.Fatal(err)
	}

	_, err = parser.AddCommand("version", command.VersionDescription, command.VersionHelp,
		&command.Version{
			Name:    name,
			Version: version,
			Build:   build,
		})
	if err != nil {
		logrus.Fatal(err)
	}

	_, err = parser.Parse()
	if err != nil {
		if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrCommandRequired {
			parser.WriteHelp(os.Stdout)
		}

		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>/*
rqlite -- replicating SQLite via the Raft consensus protocol..

rqlite is a distributed system that provides a replicated relational database,
using SQLite as the storage engine.

rqlite is written in Go and uses Raft to achieve consensus across all the
instances of the SQLite databases. rqlite ensures that every change made to
the database is made to a majority of underlying SQLite files, or none-at-all.
*/

package main

import (
	"flag"
	"fmt"
	"log"
	"net"
	"os"
	"os/signal"
	"path/filepath"
	"runtime"
	"runtime/pprof"
	"time"

	"github.com/rqlite/rqlite/auth"
	"github.com/rqlite/rqlite/cluster"
	"github.com/rqlite/rqlite/disco"
	httpd "github.com/rqlite/rqlite/http"
	"github.com/rqlite/rqlite/store"
	"github.com/rqlite/rqlite/tcp"
)

const sqliteDSN = "db.sqlite"

const logo = `
            _ _ _           _
           | (_) |         | |
  _ __ __ _| |_| |_ ___  __| |
 | '__/ _  | | | __/ _ \/ _  |  The lightweight, distributed
 | | | (_| | | | ||  __/ (_| |  relational database.
 |_|  \__, |_|_|\__\___|\__,_|
         | |
         |_|
`

// These variables are populated via the Go linker.
var (
	version   = "3"
	commit    = "unknown"
	branch    = "unknown"
	buildtime = "unknown"
)

const (
	muxRaftHeader = 1 // Raft consensus communications
	muxMetaHeader = 2 // Cluster meta communications
)

const (
	publishPeerDelay   = 1 * time.Second
	publishPeerTimeout = 30 * time.Second
)

var httpAddr string
var httpAdv string
var authFile string
var x509Cert string
var x509Key string
var raftAddr string
var raftAdv string
var joinAddr string
var noVerify bool
var discoURL string
var discoID string
var expvar bool
var pprofEnabled bool
var dsn string
var onDisk bool
var raftSnapThreshold uint64
var raftHeartbeatTimeout string
var raftApplyTimeout string
var raftOpenTimeout string
var showVersion bool
var cpuProfile string
var memProfile string

const desc = `rqlite is a distributed system that provides a replicated relational database.`

func init() {
	flag.StringVar(&httpAddr, "http", "localhost:4001", "HTTP server bind address. For HTTPS, set X.509 cert and key")
	flag.StringVar(&httpAdv, "httpadv", "", "Advertised HTTP address. If not set, same as HTTP server")
	flag.StringVar(&x509Cert, "x509cert", "", "Path to X.509 certificate")
	flag.StringVar(&x509Key, "x509key", "", "Path to X.509 private key for certificate")
	flag.StringVar(&authFile, "auth", "", "Path to authentication and authorization file. If not set, not enabled")
	flag.StringVar(&raftAddr, "raft", "localhost:4002", "Raft communication bind address")
	flag.StringVar(&raftAdv, "raftadv", "", "Advertised Raft communication address. If not set, same as Raft bind")
	flag.StringVar(&joinAddr, "join", "", "Join a cluster via node at protocol://host:port")
	flag.BoolVar(&noVerify, "noverify", false, "Skip verification of remote HTTPS cert when joining cluster")
	flag.StringVar(&discoURL, "disco", "http://discovery.rqlite.com", "Set Discovery Service URL")
	flag.StringVar(&discoID, "discoid", "", "Set Discovery ID. If not set, Discovery Service not used")
	flag.BoolVar(&expvar, "expvar", true, "Serve expvar data on HTTP server")
	flag.BoolVar(&pprofEnabled, "pprof", true, "Serve pprof data on HTTP server")
	flag.StringVar(&dsn, "dsn", "", `SQLite DSN parameters. E.g. "cache=shared&mode=memory"`)
	flag.BoolVar(&onDisk, "ondisk", false, "Use an on-disk SQLite database")
	flag.BoolVar(&showVersion, "version", false, "Show version information and exit")
	flag.StringVar(&raftHeartbeatTimeout, "rafttimeout", "1s", "Raft heartbeat timeout")
	flag.StringVar(&raftApplyTimeout, "raftapplytimeout", "10s", "Raft apply timeout")
	flag.StringVar(&raftOpenTimeout, "raftopentimeout", "120s", "Time for initial Raft logs to be applied. Use 0s duration to skip wait")
	flag.Uint64Var(&raftSnapThreshold, "raftsnap", 8192, "Number of outstanding log entries that trigger snapshot")
	flag.StringVar(&cpuProfile, "cpuprofile", "", "Path to file for CPU profiling information")
	flag.StringVar(&memProfile, "memprofile", "", "Path to file for memory profiling information")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "\n%s\n\n", desc)
		fmt.Fprintf(os.Stderr, "Usage: %s [arguments] <data directory>\n", os.Args[0])
		flag.PrintDefaults()
	}
}

func main() {
	flag.Parse()

	if showVersion {
		fmt.Printf("rqlited %s %s %s (commit %s, branch %s)\n",
			version, runtime.GOOS, runtime.GOARCH, commit, branch)
		os.Exit(0)
	}

	// Ensure the data path is set.
	if flag.NArg() == 0 {
		flag.Usage()
		os.Exit(1)
	}

	dataPath := flag.Arg(0)

	// Display logo.
	fmt.Println(logo)

	// Configure logging and pump out initial message.
	log.SetFlags(log.LstdFlags)
	log.SetOutput(os.Stderr)
	log.SetPrefix("[rqlited] ")
	log.Printf("rqlited starting, version %s, commit %s, branch %s", version, commit, branch)
	log.Printf("target architecture is %s, operating system target is %s", runtime.GOARCH, runtime.GOOS)

	// Start requested profiling.
	startProfile(cpuProfile, memProfile)

	// Set up TCP communication between nodes.
	ln, err := net.Listen("tcp", raftAddr)
	if err != nil {
		log.Fatalf("failed to listen on %s: %s", raftAddr, err.Error())
	}
	var adv net.Addr
	if raftAdv != "" {
		adv, err = net.ResolveTCPAddr("tcp", raftAdv)
		if err != nil {
			log.Fatalf("failed to resolve advertise address %s: %s", raftAdv, err.Error())
		}
	}
	mux := tcp.NewMux(ln, adv)
	go mux.Serve()

	// Start up mux and get transports for cluster.
	raftTn := mux.Listen(muxRaftHeader)

	// Create and open the store.
	dataPath, err = filepath.Abs(dataPath)
	if err != nil {
		log.Fatalf("failed to determine absolute data path: %s", err.Error())
	}
	dbConf := store.NewDBConfig(dsn, !onDisk)

	store := store.New(&store.StoreConfig{
		DBConf: dbConf,
		Dir:    dataPath,
		Tn:     raftTn,
	})

	// Set optional parameters on store.
	store.SnapshotThreshold = raftSnapThreshold
	store.HeartbeatTimeout, err = time.ParseDuration(raftHeartbeatTimeout)
	if err != nil {
		log.Fatalf("failed to parse Raft heartbeat timeout %s: %s", raftHeartbeatTimeout, err.Error())
	}
	store.ApplyTimeout, err = time.ParseDuration(raftApplyTimeout)
	if err != nil {
		log.Fatalf("failed to parse Raft apply timeout %s: %s", raftApplyTimeout, err.Error())
	}
	store.OpenTimeout, err = time.ParseDuration(raftOpenTimeout)
	if err != nil {
		log.Fatalf("failed to parse Raft open timeout %s: %s", raftOpenTimeout, err.Error())
	}

	// Determine join addresses.
	joins, err := determineJoinAddresses()
	if err != nil {
		log.Fatalf("unable to determine join addresses: %s", err.Error())
	}

	// Now, open it.
	if err := store.Open(len(joins) == 0); err != nil {
		log.Fatalf("failed to open store: %s", err.Error())
	}

	// Create and configure cluster service.
	tn := mux.Listen(muxMetaHeader)
	cs := cluster.NewService(tn, store)
	if err := cs.Open(); err != nil {
		log.Fatalf("failed to open cluster service: %s", err.Error())
	}

	// Execute any requested join operation.
	if len(joins) > 0 {
		log.Println("join addresses are:", joins)
		if store.JoinRequired() {
			advAddr := raftAddr
			if raftAdv != "" {
				advAddr = raftAdv
			}
			if j, err := cluster.Join(joins, advAddr, noVerify); err != nil {
				log.Fatalf("failed to join cluster at %s: %s", joins, err.Error())
			} else {
				log.Println("successfully joined cluster at", j)
			}
		} else {
			log.Println("node is already member of cluster, ignoring any join requests")
		}
	} else {
		log.Println("no join addresses available")
	}

	// Publish to the cluster the mapping between this Raft address and API address.
	// The Raft layer broadcasts the resolved address, so use that as the key. But
	// only set different HTTP advertise address if set.
	apiAdv := httpAddr
	if httpAdv != "" {
		apiAdv = httpAdv
	}

	if err := publishAPIAddr(cs, raftTn.Addr().String(), apiAdv, publishPeerTimeout); err != nil {
		log.Fatalf("failed to set peer for %s to %s: %s", raftAddr, httpAddr, err.Error())
	}
	log.Printf("set peer for %s to %s", raftTn.Addr().String(), apiAdv)

	// Create HTTP server and load authentication information, if supplied.
	var s *httpd.Service
	if authFile != "" {
		f, err := os.Open(authFile)
		if err != nil {
			log.Fatalf("failed to open authentication file %s: %s", authFile, err.Error())
		}
		credentialStore := auth.NewCredentialsStore()
		if err := credentialStore.Load(f); err != nil {
			log.Fatalf("failed to load authentication file: %s", err.Error())
		}
		s = httpd.New(httpAddr, store, credentialStore)
	} else {
		s = httpd.New(httpAddr, store, nil)
	}

	s.CertFile = x509Cert
	s.KeyFile = x509Key
	s.Expvar = expvar
	s.Pprof = pprofEnabled
	s.BuildInfo = map[string]interface{}{
		"commit":     commit,
		"branch":     branch,
		"version":    version,
		"build_time": buildtime,
	}
	if err := s.Start(); err != nil {
		log.Fatalf("failed to start HTTP server: %s", err.Error())
	}

	terminate := make(chan os.Signal, 1)
	signal.Notify(terminate, os.Interrupt)
	<-terminate
	if err := store.Close(true); err != nil {
		log.Printf("failed to close store: %s", err.Error())
	}
	stopProfile()
	log.Println("rqlite server stopped")
}

func determineJoinAddresses() ([]string, error) {
	apiAdv := httpAddr
	if httpAdv != "" {
		apiAdv = httpAdv
	}

	var addrs []string
	if joinAddr != "" {
		// An explicit join address is first priority.
		addrs = append(addrs, joinAddr)
	}

	if discoID != "" {
		log.Printf("registering with Discovery Service at %s with ID %s", discoURL, discoID)
		c := disco.New(discoURL)
		r, err := c.Register(discoID, apiAdv)
		if err != nil {
			return nil, err
		}
		log.Println("Discovery service responded with nodes:", r.Nodes)
		for _, a := range r.Nodes {
			if a != apiAdv {
				// Only other nodes can be joined.
				addrs = append(addrs, a)
			}
		}
	}

	return addrs, nil
}

func publishAPIAddr(c *cluster.Service, raftAddr, apiAddr string, t time.Duration) error {
	tck := time.NewTicker(publishPeerDelay)
	defer tck.Stop()
	tmr := time.NewTimer(t)
	defer tmr.Stop()

	for {
		select {
		case <-tck.C:
			if err := c.SetPeer(raftAddr, apiAddr); err != nil {
				log.Printf("failed to set peer for %s to %s: %s (retrying)",
					raftAddr, apiAddr, err.Error())
				continue
			}
			return nil
		case <-tmr.C:
			return fmt.Errorf("set peer timeout expired")
		}
	}
}

// prof stores the file locations of active profiles.
var prof struct {
	cpu *os.File
	mem *os.File
}

// startProfile initializes the CPU and memory profile, if specified.
func startProfile(cpuprofile, memprofile string) {
	if cpuprofile != "" {
		f, err := os.Create(cpuprofile)
		if err != nil {
			log.Fatalf("failed to create CPU profile file at %s: %s", cpuprofile, err.Error())
		}
		log.Printf("writing CPU profile to: %s\n", cpuprofile)
		prof.cpu = f
		pprof.StartCPUProfile(prof.cpu)
	}

	if memprofile != "" {
		f, err := os.Create(memprofile)
		if err != nil {
			log.Fatalf("failed to create memory profile file at %s: %s", cpuprofile, err.Error())
		}
		log.Printf("writing memory profile to: %s\n", memprofile)
		prof.mem = f
		runtime.MemProfileRate = 4096
	}
}

// stopProfile closes the CPU and memory profiles if they are running.
func stopProfile() {
	if prof.cpu != nil {
		pprof.StopCPUProfile()
		prof.cpu.Close()
		log.Println("CPU profiling stopped")
	}
	if prof.mem != nil {
		pprof.Lookup("heap").WriteTo(prof.mem, 0)
		prof.mem.Close()
		log.Println("memory profiling stopped")
	}
}
<commit_msg>Disco service is capitalized in logs<commit_after>/*
rqlite -- replicating SQLite via the Raft consensus protocol..

rqlite is a distributed system that provides a replicated relational database,
using SQLite as the storage engine.

rqlite is written in Go and uses Raft to achieve consensus across all the
instances of the SQLite databases. rqlite ensures that every change made to
the database is made to a majority of underlying SQLite files, or none-at-all.
*/

package main

import (
	"flag"
	"fmt"
	"log"
	"net"
	"os"
	"os/signal"
	"path/filepath"
	"runtime"
	"runtime/pprof"
	"time"

	"github.com/rqlite/rqlite/auth"
	"github.com/rqlite/rqlite/cluster"
	"github.com/rqlite/rqlite/disco"
	httpd "github.com/rqlite/rqlite/http"
	"github.com/rqlite/rqlite/store"
	"github.com/rqlite/rqlite/tcp"
)

const sqliteDSN = "db.sqlite"

const logo = `
            _ _ _           _
           | (_) |         | |
  _ __ __ _| |_| |_ ___  __| |
 | '__/ _  | | | __/ _ \/ _  |  The lightweight, distributed
 | | | (_| | | | ||  __/ (_| |  relational database.
 |_|  \__, |_|_|\__\___|\__,_|
         | |
         |_|
`

// These variables are populated via the Go linker.
var (
	version   = "3"
	commit    = "unknown"
	branch    = "unknown"
	buildtime = "unknown"
)

const (
	muxRaftHeader = 1 // Raft consensus communications
	muxMetaHeader = 2 // Cluster meta communications
)

const (
	publishPeerDelay   = 1 * time.Second
	publishPeerTimeout = 30 * time.Second
)

var httpAddr string
var httpAdv string
var authFile string
var x509Cert string
var x509Key string
var raftAddr string
var raftAdv string
var joinAddr string
var noVerify bool
var discoURL string
var discoID string
var expvar bool
var pprofEnabled bool
var dsn string
var onDisk bool
var raftSnapThreshold uint64
var raftHeartbeatTimeout string
var raftApplyTimeout string
var raftOpenTimeout string
var showVersion bool
var cpuProfile string
var memProfile string

const desc = `rqlite is a distributed system that provides a replicated relational database.`

func init() {
	flag.StringVar(&httpAddr, "http", "localhost:4001", "HTTP server bind address. For HTTPS, set X.509 cert and key")
	flag.StringVar(&httpAdv, "httpadv", "", "Advertised HTTP address. If not set, same as HTTP server")
	flag.StringVar(&x509Cert, "x509cert", "", "Path to X.509 certificate")
	flag.StringVar(&x509Key, "x509key", "", "Path to X.509 private key for certificate")
	flag.StringVar(&authFile, "auth", "", "Path to authentication and authorization file. If not set, not enabled")
	flag.StringVar(&raftAddr, "raft", "localhost:4002", "Raft communication bind address")
	flag.StringVar(&raftAdv, "raftadv", "", "Advertised Raft communication address. If not set, same as Raft bind")
	flag.StringVar(&joinAddr, "join", "", "Join a cluster via node at protocol://host:port")
	flag.BoolVar(&noVerify, "noverify", false, "Skip verification of remote HTTPS cert when joining cluster")
	flag.StringVar(&discoURL, "disco", "http://discovery.rqlite.com", "Set Discovery Service URL")
	flag.StringVar(&discoID, "discoid", "", "Set Discovery ID. If not set, Discovery Service not used")
	flag.BoolVar(&expvar, "expvar", true, "Serve expvar data on HTTP server")
	flag.BoolVar(&pprofEnabled, "pprof", true, "Serve pprof data on HTTP server")
	flag.StringVar(&dsn, "dsn", "", `SQLite DSN parameters. E.g. "cache=shared&mode=memory"`)
	flag.BoolVar(&onDisk, "ondisk", false, "Use an on-disk SQLite database")
	flag.BoolVar(&showVersion, "version", false, "Show version information and exit")
	flag.StringVar(&raftHeartbeatTimeout, "rafttimeout", "1s", "Raft heartbeat timeout")
	flag.StringVar(&raftApplyTimeout, "raftapplytimeout", "10s", "Raft apply timeout")
	flag.StringVar(&raftOpenTimeout, "raftopentimeout", "120s", "Time for initial Raft logs to be applied. Use 0s duration to skip wait")
	flag.Uint64Var(&raftSnapThreshold, "raftsnap", 8192, "Number of outstanding log entries that trigger snapshot")
	flag.StringVar(&cpuProfile, "cpuprofile", "", "Path to file for CPU profiling information")
	flag.StringVar(&memProfile, "memprofile", "", "Path to file for memory profiling information")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "\n%s\n\n", desc)
		fmt.Fprintf(os.Stderr, "Usage: %s [arguments] <data directory>\n", os.Args[0])
		flag.PrintDefaults()
	}
}

func main() {
	flag.Parse()

	if showVersion {
		fmt.Printf("rqlited %s %s %s (commit %s, branch %s)\n",
			version, runtime.GOOS, runtime.GOARCH, commit, branch)
		os.Exit(0)
	}

	// Ensure the data path is set.
	if flag.NArg() == 0 {
		flag.Usage()
		os.Exit(1)
	}

	dataPath := flag.Arg(0)

	// Display logo.
	fmt.Println(logo)

	// Configure logging and pump out initial message.
	log.SetFlags(log.LstdFlags)
	log.SetOutput(os.Stderr)
	log.SetPrefix("[rqlited] ")
	log.Printf("rqlited starting, version %s, commit %s, branch %s", version, commit, branch)
	log.Printf("target architecture is %s, operating system target is %s", runtime.GOARCH, runtime.GOOS)

	// Start requested profiling.
	startProfile(cpuProfile, memProfile)

	// Set up TCP communication between nodes.
	ln, err := net.Listen("tcp", raftAddr)
	if err != nil {
		log.Fatalf("failed to listen on %s: %s", raftAddr, err.Error())
	}
	var adv net.Addr
	if raftAdv != "" {
		adv, err = net.ResolveTCPAddr("tcp", raftAdv)
		if err != nil {
			log.Fatalf("failed to resolve advertise address %s: %s", raftAdv, err.Error())
		}
	}
	mux := tcp.NewMux(ln, adv)
	go mux.Serve()

	// Start up mux and get transports for cluster.
	raftTn := mux.Listen(muxRaftHeader)

	// Create and open the store.
	dataPath, err = filepath.Abs(dataPath)
	if err != nil {
		log.Fatalf("failed to determine absolute data path: %s", err.Error())
	}
	dbConf := store.NewDBConfig(dsn, !onDisk)

	store := store.New(&store.StoreConfig{
		DBConf: dbConf,
		Dir:    dataPath,
		Tn:     raftTn,
	})

	// Set optional parameters on store.
	store.SnapshotThreshold = raftSnapThreshold
	store.HeartbeatTimeout, err = time.ParseDuration(raftHeartbeatTimeout)
	if err != nil {
		log.Fatalf("failed to parse Raft heartbeat timeout %s: %s", raftHeartbeatTimeout, err.Error())
	}
	store.ApplyTimeout, err = time.ParseDuration(raftApplyTimeout)
	if err != nil {
		log.Fatalf("failed to parse Raft apply timeout %s: %s", raftApplyTimeout, err.Error())
	}
	store.OpenTimeout, err = time.ParseDuration(raftOpenTimeout)
	if err != nil {
		log.Fatalf("failed to parse Raft open timeout %s: %s", raftOpenTimeout, err.Error())
	}

	// Determine join addresses.
	joins, err := determineJoinAddresses()
	if err != nil {
		log.Fatalf("unable to determine join addresses: %s", err.Error())
	}

	// Now, open it.
	if err := store.Open(len(joins) == 0); err != nil {
		log.Fatalf("failed to open store: %s", err.Error())
	}

	// Create and configure cluster service.
	tn := mux.Listen(muxMetaHeader)
	cs := cluster.NewService(tn, store)
	if err := cs.Open(); err != nil {
		log.Fatalf("failed to open cluster service: %s", err.Error())
	}

	// Execute any requested join operation.
	if len(joins) > 0 {
		log.Println("join addresses are:", joins)
		if store.JoinRequired() {
			advAddr := raftAddr
			if raftAdv != "" {
				advAddr = raftAdv
			}
			if j, err := cluster.Join(joins, advAddr, noVerify); err != nil {
				log.Fatalf("failed to join cluster at %s: %s", joins, err.Error())
			} else {
				log.Println("successfully joined cluster at", j)
			}
		} else {
			log.Println("node is already member of cluster, ignoring any join requests")
		}
	} else {
		log.Println("no join addresses available")
	}

	// Publish to the cluster the mapping between this Raft address and API address.
	// The Raft layer broadcasts the resolved address, so use that as the key. But
	// only set different HTTP advertise address if set.
	apiAdv := httpAddr
	if httpAdv != "" {
		apiAdv = httpAdv
	}

	if err := publishAPIAddr(cs, raftTn.Addr().String(), apiAdv, publishPeerTimeout); err != nil {
		log.Fatalf("failed to set peer for %s to %s: %s", raftAddr, httpAddr, err.Error())
	}
	log.Printf("set peer for %s to %s", raftTn.Addr().String(), apiAdv)

	// Create HTTP server and load authentication information, if supplied.
	var s *httpd.Service
	if authFile != "" {
		f, err := os.Open(authFile)
		if err != nil {
			log.Fatalf("failed to open authentication file %s: %s", authFile, err.Error())
		}
		credentialStore := auth.NewCredentialsStore()
		if err := credentialStore.Load(f); err != nil {
			log.Fatalf("failed to load authentication file: %s", err.Error())
		}
		s = httpd.New(httpAddr, store, credentialStore)
	} else {
		s = httpd.New(httpAddr, store, nil)
	}

	s.CertFile = x509Cert
	s.KeyFile = x509Key
	s.Expvar = expvar
	s.Pprof = pprofEnabled
	s.BuildInfo = map[string]interface{}{
		"commit":     commit,
		"branch":     branch,
		"version":    version,
		"build_time": buildtime,
	}
	if err := s.Start(); err != nil {
		log.Fatalf("failed to start HTTP server: %s", err.Error())
	}

	terminate := make(chan os.Signal, 1)
	signal.Notify(terminate, os.Interrupt)
	<-terminate
	if err := store.Close(true); err != nil {
		log.Printf("failed to close store: %s", err.Error())
	}
	stopProfile()
	log.Println("rqlite server stopped")
}

func determineJoinAddresses() ([]string, error) {
	apiAdv := httpAddr
	if httpAdv != "" {
		apiAdv = httpAdv
	}

	var addrs []string
	if joinAddr != "" {
		// An explicit join address is first priority.
		addrs = append(addrs, joinAddr)
	}

	if discoID != "" {
		log.Printf("registering with Discovery Service at %s with ID %s", discoURL, discoID)
		c := disco.New(discoURL)
		r, err := c.Register(discoID, apiAdv)
		if err != nil {
			return nil, err
		}
		log.Println("Discovery Service responded with nodes:", r.Nodes)
		for _, a := range r.Nodes {
			if a != apiAdv {
				// Only other nodes can be joined.
				addrs = append(addrs, a)
			}
		}
	}

	return addrs, nil
}

func publishAPIAddr(c *cluster.Service, raftAddr, apiAddr string, t time.Duration) error {
	tck := time.NewTicker(publishPeerDelay)
	defer tck.Stop()
	tmr := time.NewTimer(t)
	defer tmr.Stop()

	for {
		select {
		case <-tck.C:
			if err := c.SetPeer(raftAddr, apiAddr); err != nil {
				log.Printf("failed to set peer for %s to %s: %s (retrying)",
					raftAddr, apiAddr, err.Error())
				continue
			}
			return nil
		case <-tmr.C:
			return fmt.Errorf("set peer timeout expired")
		}
	}
}

// prof stores the file locations of active profiles.
var prof struct {
	cpu *os.File
	mem *os.File
}

// startProfile initializes the CPU and memory profile, if specified.
func startProfile(cpuprofile, memprofile string) {
	if cpuprofile != "" {
		f, err := os.Create(cpuprofile)
		if err != nil {
			log.Fatalf("failed to create CPU profile file at %s: %s", cpuprofile, err.Error())
		}
		log.Printf("writing CPU profile to: %s\n", cpuprofile)
		prof.cpu = f
		pprof.StartCPUProfile(prof.cpu)
	}

	if memprofile != "" {
		f, err := os.Create(memprofile)
		if err != nil {
			log.Fatalf("failed to create memory profile file at %s: %s", cpuprofile, err.Error())
		}
		log.Printf("writing memory profile to: %s\n", memprofile)
		prof.mem = f
		runtime.MemProfileRate = 4096
	}
}

// stopProfile closes the CPU and memory profiles if they are running.
func stopProfile() {
	if prof.cpu != nil {
		pprof.StopCPUProfile()
		prof.cpu.Close()
		log.Println("CPU profiling stopped")
	}
	if prof.mem != nil {
		pprof.Lookup("heap").WriteTo(prof.mem, 0)
		prof.mem.Close()
		log.Println("memory profiling stopped")
	}
}
<|endoftext|>


<commit_before>package main

/*import (
	"fmt"
	"strconv"
)*/

func FieldIsStoreUUID(name string, value string) (interface{}, error) {
	o, _, err := FindStore(value, false)
	if err != nil {
		return nil, err
	}
	return o.UUID, nil
}

func FieldIsTargetUUID(name string, value string) (interface{}, error) {
	o, _, err := FindTarget(value, false)
	if err != nil {
		return nil, err
	}
	return o.UUID, nil
}

func FieldIsRetentionPolicyUUID(name string, value string) (interface{}, error) {
	o, _, err := FindRetentionPolicy(value, false)
	if err != nil {
		return nil, err
	}
	return o.UUID, nil
}

func FieldIsScheduleUUID(name string, value string) (interface{}, error) {
	o, _, err := FindSchedule(value, false)
	if err != nil {
		return nil, err
	}
	return o.UUID, nil
}

func FieldIsRetentionTimeframe(name string, value string) (interface{}, error) {
	i, err := ParseDuration(value)
	if err != nil {
		return value, err
	}
	return i, nil
}
<commit_msg>Removed commented out block<commit_after>package main

func FieldIsStoreUUID(name string, value string) (interface{}, error) {
	o, _, err := FindStore(value, false)
	if err != nil {
		return nil, err
	}
	return o.UUID, nil
}

func FieldIsTargetUUID(name string, value string) (interface{}, error) {
	o, _, err := FindTarget(value, false)
	if err != nil {
		return nil, err
	}
	return o.UUID, nil
}

func FieldIsRetentionPolicyUUID(name string, value string) (interface{}, error) {
	o, _, err := FindRetentionPolicy(value, false)
	if err != nil {
		return nil, err
	}
	return o.UUID, nil
}

func FieldIsScheduleUUID(name string, value string) (interface{}, error) {
	o, _, err := FindSchedule(value, false)
	if err != nil {
		return nil, err
	}
	return o.UUID, nil
}

func FieldIsRetentionTimeframe(name string, value string) (interface{}, error) {
	i, err := ParseDuration(value)
	if err != nil {
		return value, err
	}
	return i, nil
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"log"
	"net"
	"net/http"
	_ "net/http/pprof"
	"os"
	"strings"

	_ "github.com/anacrolix/envpprof"

	"github.com/anacrolix/libtorgo/metainfo"

	"github.com/anacrolix/torrent"
)

var (
	downloadDir = flag.String("downloadDir", "", "directory to store download torrent data")
	testPeer    = flag.String("testPeer", "", "bootstrap peer address")
	// TODO: Check the default torrent listen port.
	listenAddr      = flag.String("listenAddr", ":50007", "incoming connection address")
	disableTrackers = flag.Bool("disableTrackers", false, "disable trackers")
	seed            = flag.Bool("seed", false, "seed after downloading")
	upload          = flag.Bool("upload", true, "upload data to peers")
)

func usage() {
	fmt.Fprintf(os.Stderr, "Usage: %s [flags] (magnet URI or .torrent file path)...\n", os.Args[0])
	os.Stderr.WriteString("Download using the BitTorrent network.\n")

	flag.PrintDefaults()
}

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	flag.Usage = usage
	flag.Parse()
	client, err := torrent.NewClient(&torrent.Config{
		DataDir:         *downloadDir,
		DisableTrackers: *disableTrackers,
		ListenAddr:      *listenAddr,
		NoUpload:        !*upload,
	})
	if err != nil {
		log.Fatalf("error creating client: %s", err)
	}
	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		client.WriteStatus(w)
	})
	defer client.Close()
	if flag.NArg() == 0 {
		fmt.Fprintln(os.Stderr, "no torrents specified")
		return
	}
	for _, arg := range flag.Args() {
		t := func() torrent.Torrent {
			if strings.HasPrefix(arg, "magnet:") {
				t, err := client.AddMagnet(arg)
				if err != nil {
					log.Fatalf("error adding magnet: %s", err)
				}
				return t
			} else {
				metaInfo, err := metainfo.LoadFromFile(arg)
				if err != nil {
					log.Fatal(err)
				}
				t, err := client.AddTorrent(metaInfo)
				if err != nil {
					log.Fatal(err)
				}
				return t
			}
		}()
		// client.PrioritizeDataRegion(ih, 0, 999999999)
		err := t.AddPeers(func() []torrent.Peer {
			if *testPeer == "" {
				return nil
			}
			addr, err := net.ResolveTCPAddr("tcp", *testPeer)
			if err != nil {
				log.Fatal(err)
			}
			return []torrent.Peer{{
				IP:   addr.IP,
				Port: addr.Port,
			}}
		}())
		if err != nil {
			log.Fatal(err)
		}
		go func() {
			<-t.GotMetainfo
			t.DownloadAll()
		}()
	}
	if *seed {
		select {}
	}
	if client.WaitAll() {
		log.Print("downloaded ALL the torrents")
	} else {
		log.Fatal("y u no complete torrents?!")
	}
}
<commit_msg>add cli flag to disable DHT<commit_after>package main

import (
	"flag"
	"fmt"
	"log"
	"net"
	"net/http"
	_ "net/http/pprof"
	"os"
	"strings"

	_ "github.com/anacrolix/envpprof"

	"github.com/anacrolix/libtorgo/metainfo"

	"github.com/anacrolix/torrent"
)

var (
	downloadDir = flag.String("downloadDir", "", "directory to store download torrent data")
	testPeer    = flag.String("testPeer", "", "bootstrap peer address")
	// TODO: Check the default torrent listen port.
	listenAddr      = flag.String("listenAddr", ":50007", "incoming connection address")
	disableTrackers = flag.Bool("disableTrackers", false, "disable trackers")
	disableDHT      = flag.Bool("disableDHT", false, "disable DHT")
	seed            = flag.Bool("seed", false, "seed after downloading")
	upload          = flag.Bool("upload", true, "upload data to peers")
)

func usage() {
	fmt.Fprintf(os.Stderr, "Usage: %s [flags] (magnet URI or .torrent file path)...\n", os.Args[0])
	os.Stderr.WriteString("Download using the BitTorrent network.\n")

	flag.PrintDefaults()
}

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	flag.Usage = usage
	flag.Parse()
	client, err := torrent.NewClient(&torrent.Config{
		DataDir:         *downloadDir,
		DisableTrackers: *disableTrackers,
		ListenAddr:      *listenAddr,
		NoUpload:        !*upload,
		NoDHT:           *disableDHT,
	})
	if err != nil {
		log.Fatalf("error creating client: %s", err)
	}
	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		client.WriteStatus(w)
	})
	defer client.Close()
	if flag.NArg() == 0 {
		fmt.Fprintln(os.Stderr, "no torrents specified")
		return
	}
	for _, arg := range flag.Args() {
		t := func() torrent.Torrent {
			if strings.HasPrefix(arg, "magnet:") {
				t, err := client.AddMagnet(arg)
				if err != nil {
					log.Fatalf("error adding magnet: %s", err)
				}
				return t
			} else {
				metaInfo, err := metainfo.LoadFromFile(arg)
				if err != nil {
					log.Fatal(err)
				}
				t, err := client.AddTorrent(metaInfo)
				if err != nil {
					log.Fatal(err)
				}
				return t
			}
		}()
		// client.PrioritizeDataRegion(ih, 0, 999999999)
		err := t.AddPeers(func() []torrent.Peer {
			if *testPeer == "" {
				return nil
			}
			addr, err := net.ResolveTCPAddr("tcp", *testPeer)
			if err != nil {
				log.Fatal(err)
			}
			return []torrent.Peer{{
				IP:   addr.IP,
				Port: addr.Port,
			}}
		}())
		if err != nil {
			log.Fatal(err)
		}
		go func() {
			<-t.GotMetainfo
			t.DownloadAll()
		}()
	}
	if *seed {
		select {}
	}
	if client.WaitAll() {
		log.Print("downloaded ALL the torrents")
	} else {
		log.Fatal("y u no complete torrents?!")
	}
}
<|endoftext|>


<commit_before>/*
Copyright 2018 The Knative Authors

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

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

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

package main

import (
	"flag"
	"log"

	"k8s.io/client-go/tools/clientcmd"

	"go.uber.org/zap"

	"github.com/knative/pkg/configmap"
	"github.com/knative/pkg/logging/logkey"
	"github.com/knative/pkg/signals"
	"github.com/knative/pkg/version"
	"github.com/knative/pkg/webhook"
	kpa "github.com/knative/serving/pkg/apis/autoscaling/v1alpha1"
	net "github.com/knative/serving/pkg/apis/networking/v1alpha1"
	"github.com/knative/serving/pkg/apis/serving/v1alpha1"
	"github.com/knative/serving/pkg/logging"
	"github.com/knative/serving/pkg/system"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/client-go/kubernetes"
)

const (
	component = "webhook"
)

var (
	masterURL  = flag.String("master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
	kubeconfig = flag.String("kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
)

func main() {
	flag.Parse()
	cm, err := configmap.Load("/etc/config-logging")
	if err != nil {
		log.Fatalf("Error loading logging configuration: %v", err)
	}
	config, err := logging.NewConfigFromMap(cm)
	if err != nil {
		log.Fatalf("Error parsing logging configuration: %v", err)
	}
	logger, atomicLevel := logging.NewLoggerFromConfig(config, component)
	defer logger.Sync()
	logger = logger.With(zap.String(logkey.ControllerType, "webhook"))

	logger.Info("Starting the Configuration Webhook")

	// set up signals so we handle the first shutdown signal gracefully
	stopCh := signals.SetupSignalHandler()

	clusterConfig, err := clientcmd.BuildConfigFromFlags(*masterURL, *kubeconfig)
	if err != nil {
		logger.Fatal("Failed to get cluster config", zap.Error(err))
	}

	kubeClient, err := kubernetes.NewForConfig(clusterConfig)
	if err != nil {
		logger.Fatal("Failed to get the client set", zap.Error(err))
	}

	if err := version.CheckMinimumVersion(kubeClient.Discovery()); err != nil {
		logger.Fatalf("Version check failed: %v", err)
	}

	// Watch the logging config map and dynamically update logging levels.
	configMapWatcher := configmap.NewInformedWatcher(kubeClient, system.Namespace())
	configMapWatcher.Watch(logging.ConfigName, logging.UpdateLevelFromConfigMap(logger, atomicLevel, component))
	if err = configMapWatcher.Start(stopCh); err != nil {
		logger.Fatalf("failed to start configuration manager: %v", err)
	}

	options := webhook.ControllerOptions{
		ServiceName:    "webhook",
		DeploymentName: "webhook",
		Namespace:      system.Namespace(),
		Port:           443,
		SecretName:     "webhook-certs",
		WebhookName:    "webhook.serving.knative.dev",
	}
	controller := webhook.AdmissionController{
		Client:  kubeClient,
		Options: options,
		Handlers: map[schema.GroupVersionKind]webhook.GenericCRD{
			v1alpha1.SchemeGroupVersion.WithKind("Revision"):      &v1alpha1.Revision{},
			v1alpha1.SchemeGroupVersion.WithKind("Configuration"): &v1alpha1.Configuration{},
			v1alpha1.SchemeGroupVersion.WithKind("Route"):         &v1alpha1.Route{},
			v1alpha1.SchemeGroupVersion.WithKind("Service"):       &v1alpha1.Service{},
			kpa.SchemeGroupVersion.WithKind("PodAutoscaler"):      &kpa.PodAutoscaler{},
			net.SchemeGroupVersion.WithKind("ClusterIngress"):     &net.ClusterIngress{},
		},
		Logger: logger,
	}
	if err != nil {
		logger.Fatal("Failed to create the admission controller", zap.Error(err))
	}
	controller.Run(stopCh)
}
<commit_msg>Minor cleanup of the webhook (#2886)<commit_after>/*
Copyright 2018 The Knative Authors

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

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

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

package main

import (
	"flag"
	"log"

	"k8s.io/client-go/tools/clientcmd"

	"go.uber.org/zap"

	"github.com/knative/pkg/configmap"
	"github.com/knative/pkg/logging/logkey"
	"github.com/knative/pkg/signals"
	"github.com/knative/pkg/version"
	"github.com/knative/pkg/webhook"
	kpa "github.com/knative/serving/pkg/apis/autoscaling/v1alpha1"
	net "github.com/knative/serving/pkg/apis/networking/v1alpha1"
	"github.com/knative/serving/pkg/apis/serving/v1alpha1"
	"github.com/knative/serving/pkg/logging"
	"github.com/knative/serving/pkg/system"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/client-go/kubernetes"
)

const (
	component = "webhook"
)

var (
	masterURL  = flag.String("master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
	kubeconfig = flag.String("kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
)

func main() {
	flag.Parse()
	cm, err := configmap.Load("/etc/config-logging")
	if err != nil {
		log.Fatalf("Error loading logging configuration: %v", err)
	}
	config, err := logging.NewConfigFromMap(cm)
	if err != nil {
		log.Fatalf("Error parsing logging configuration: %v", err)
	}
	logger, atomicLevel := logging.NewLoggerFromConfig(config, component)
	defer logger.Sync()
	logger = logger.With(zap.String(logkey.ControllerType, component))

	logger.Info("Starting the Configuration Webhook")

	// Set up signals so we handle the first shutdown signal gracefully.
	stopCh := signals.SetupSignalHandler()

	clusterConfig, err := clientcmd.BuildConfigFromFlags(*masterURL, *kubeconfig)
	if err != nil {
		logger.Fatalw("Failed to get cluster config", zap.Error(err))
	}

	kubeClient, err := kubernetes.NewForConfig(clusterConfig)
	if err != nil {
		logger.Fatalw("Failed to get the client set", zap.Error(err))
	}

	if err := version.CheckMinimumVersion(kubeClient.Discovery()); err != nil {
		logger.Fatalf("Version check failed: %v", err)
	}

	// Watch the logging config map and dynamically update logging levels.
	configMapWatcher := configmap.NewInformedWatcher(kubeClient, system.Namespace())
	configMapWatcher.Watch(logging.ConfigName, logging.UpdateLevelFromConfigMap(logger, atomicLevel, component))
	if err = configMapWatcher.Start(stopCh); err != nil {
		logger.Fatalw("Failed to start the ConfigMap watcher", zap.Error(err))
	}

	options := webhook.ControllerOptions{
		ServiceName:    "webhook",
		DeploymentName: "webhook",
		Namespace:      system.Namespace(),
		Port:           443,
		SecretName:     "webhook-certs",
		WebhookName:    "webhook.serving.knative.dev",
	}
	controller := webhook.AdmissionController{
		Client:  kubeClient,
		Options: options,
		Handlers: map[schema.GroupVersionKind]webhook.GenericCRD{
			v1alpha1.SchemeGroupVersion.WithKind("Revision"):      &v1alpha1.Revision{},
			v1alpha1.SchemeGroupVersion.WithKind("Configuration"): &v1alpha1.Configuration{},
			v1alpha1.SchemeGroupVersion.WithKind("Route"):         &v1alpha1.Route{},
			v1alpha1.SchemeGroupVersion.WithKind("Service"):       &v1alpha1.Service{},
			kpa.SchemeGroupVersion.WithKind("PodAutoscaler"):      &kpa.PodAutoscaler{},
			net.SchemeGroupVersion.WithKind("ClusterIngress"):     &net.ClusterIngress{},
		},
		Logger: logger,
	}
	if err = controller.Run(stopCh); err != nil {
		logger.Fatalw("Failed to start the admission controller", zap.Error(err))
	}
}
<|endoftext|>


<commit_before>package database

import (
	"database/sql"
	"errors"
	"fmt"
	_ "github.com/alexbrainman/odbc"
	"github.com/blackss2/utility/convert"
	"github.com/cznic/ql"
	_ "github.com/denisenkom/go-mssqldb"
	_ "github.com/go-sql-driver/mysql"
	_ "github.com/ziutek/mymysql/godrv"
	"runtime"
	"strings"
)

type Database struct {
	inst       *sql.DB
	instQL     *ql.DB
	connString string
	driver     string
}

func (db *Database) Open(driver string, connString string) error {
	db.driver = driver
	db.connString = connString
	runtime.SetFinalizer(db, func(f interface{}) {
		f.(*Database).Close()
	})
	return db.executeOpen()
}

func (db *Database) executeOpen() error {
	var err error
	if db.driver == "ql" {
		if db.connString == "mem" {
			db.instQL, err = ql.OpenMem()
		} else {
			db.instQL, err = ql.OpenFile(db.connString, nil)
		}
	} else {
		db.inst, err = sql.Open(db.driver, db.connString)
	}
	return err
}

func (db *Database) Close() error {
	var err error

	if db.inst != nil {
		err = db.inst.Close()
	} else if db.instQL != nil {
		err = db.instQL.Close()
	}
	return err
}

type Rows struct {
	inst    *sql.Rows
	qlRows  [][]interface{}
	qlIndex int
	isFirst bool
	isNil   bool
	Cols    []string
}

func (db *Database) prepare(queryStr string, retCount int) (*sql.Stmt, error) {
	stmt, err := db.inst.Prepare(queryStr)
	if err != nil {
		db.Close()
		db.executeOpen()
		if retCount > 0 {
			return db.prepare(queryStr, retCount-1)
		}
		return nil, err
	}
	return stmt, err
}

func (db *Database) Query(queryStr string) (*Rows, error) {
	rows := &Rows{nil, nil, 0, true, false, make([]string, 0, 100)}

	QUERYSTR := strings.ToUpper(queryStr)

	if db.inst != nil {
		stmt, err := db.prepare(queryStr, 1)
		if stmt != nil {
			defer stmt.Close()
		}
		if err != nil {
			db.Close()
			db.executeOpen()
			return db.TempQuery(queryStr)
		}
		rows.inst, err = stmt.Query()

		if err != nil {
			if err.Error() != "Stmt did not create a result set" {
				db.Close()
				db.executeOpen()
				return db.TempQuery(queryStr)
			} else {
				runtime.SetFinalizer(rows, func(f interface{}) {
					f.(*Rows).Close()
				})
				return rows, nil
			}
		}

		rows.Cols, err = rows.inst.Columns()

		if !rows.inst.Next() {
			rows.Close()
		} else {
			runtime.SetFinalizer(rows, func(f interface{}) {
				f.(*Rows).Close()
			})
		}
	} else if db.instQL != nil {
		if !strings.Contains(QUERYSTR, "TRANSACTION") && (strings.Contains(QUERYSTR, "INSERT") || strings.Contains(QUERYSTR, "CREATE") || strings.Contains(QUERYSTR, "UPDATE") || strings.Contains(QUERYSTR, "DELETE")) {
			queryStr = fmt.Sprintf(`
				BEGIN TRANSACTION;
					%s;
				COMMIT;
			`, queryStr)
		}

		ctx := ql.NewRWCtx()
		rs, _, err := db.instQL.Run(ctx, queryStr, nil)
		if err != nil {
			println("P1 : ", err.Error(), "\n", queryStr)
			return nil, err
		}

		if len(rs) == 0 {
			rows.isNil = true
			rows.isFirst = false
			return rows, nil
		}

		rows.Cols, err = rs[0].Fields()

		rows.qlRows, err = rs[0].Rows(-1, -1)
		if len(rows.qlRows) == 0 || err != nil {
			rows.Close()
		} else {
			runtime.SetFinalizer(rows, func(f interface{}) {
				f.(*Rows).Close()
			})
		}
	} else {
		return nil, errors.New("db is not initialized")
	}

	if strings.HasPrefix(QUERYSTR, "INSERT") && strings.Contains(QUERYSTR, "OUTPUT") && strings.Contains(QUERYSTR, "INSERTED.") {
		if rows.IsNil() {
			return nil, errors.New("insert.fail")
		}
	}
	return rows, nil
}

func (db *Database) TempQuery(queryStr string) (*Rows, error) {
	rows := &Rows{nil, nil, 0, true, false, make([]string, 0, 100)}

	if db.inst != nil {
		stmt, err := db.prepare(queryStr, 1)
		if stmt != nil {
			defer stmt.Close()
		}
		if err != nil {
			println("P1 : ", err.Error())
			return nil, err
		}
		rows.inst, err = stmt.Query()

		if err != nil {
			if err.Error() != "Stmt did not create a result set" {
				println("P2 : ", err.Error(), "\n", queryStr)
				return nil, err
			} else {
				runtime.SetFinalizer(rows, func(f interface{}) {
					f.(*Rows).Close()
				})
				return rows, nil
			}
		}

		rows.Cols, err = rows.inst.Columns()
		if err != nil {
			println("P2 : ", err.Error(), "\n", queryStr)
			return nil, err
		}

		if !rows.inst.Next() {
			rows.Close()
		} else {
			runtime.SetFinalizer(rows, func(f interface{}) {
				f.(*Rows).Close()
			})
		}
	} else if db.instQL != nil {
		return nil, errors.New("ql not use TempQuery")
	} else {
		return nil, errors.New("db is not initialized")
	}

	QUERYSTR := strings.ToUpper(queryStr)
	if strings.HasPrefix(QUERYSTR, "INSERT") && strings.Contains(QUERYSTR, "OUTPUT") && strings.Contains(QUERYSTR, "INSERTED.") {
		if rows.IsNil() {
			return nil, errors.New("insert.fail")
		}
	}

	return rows, nil
}

func (rows *Rows) Next() bool {
	if !rows.isNil && rows.isFirst {
		rows.isFirst = false
		return true
	}
	if rows.inst != nil {
		if !rows.inst.Next() {
			rows.Close()
		}
	} else if rows.qlRows != nil {
		rows.qlIndex++
		if len(rows.qlRows) <= rows.qlIndex {
			rows.Close()
		}
	} else {
		return false
	}
	return !rows.isNil
}

func (rows *Rows) FetchArray() []interface{} {
	if rows.isNil {
		return nil
	}
	if rows.inst != nil {
		cols, err := rows.inst.Columns()
		if err != nil {
			return nil
		}

		rawResult := make([]*interface{}, len(cols))
		result := make([]interface{}, len(cols))

		dest := make([]interface{}, len(cols))
		for i, _ := range rawResult {
			dest[i] = &rawResult[i]
		}
		rows.inst.Scan(dest...)
		for i, raw := range rawResult {
			if raw != nil {
				v := (*raw)
				switch v.(type) {
				case []byte:
					v = convert.String(v)
				}
				result[i] = v
			} else {
				result[i] = nil
			}
		}
		return result
	} else if rows.qlRows != nil {
		if len(rows.qlRows) <= rows.qlIndex {
			return nil
		}
		return rows.qlRows[rows.qlIndex]
	} else {
		return nil
	}

}

func (rows *Rows) FetchHash() map[string]interface{} {
	if rows.isNil {
		return nil
	}
	cols, err := rows.inst.Columns()
	if err != nil {
		return nil
	}

	result := make(map[string]interface{}, len(cols))

	row := rows.FetchArray()

	for i, v := range row {
		if v != nil {
			switch v.(type) {
			case []byte:
				v = convert.String(v)
			}
		}
		result[cols[i]] = v
		result[strings.ToUpper(cols[i])] = v
		result[strings.ToLower(cols[i])] = v
	}
	return result
}

func (rows *Rows) Close() error {
	if rows != nil {
		rows.isNil = true
		if rows.inst != nil {
			return rows.inst.Close()
		}
	}
	return nil
}

func (rows *Rows) IsNil() bool {
	return rows.isNil
}
<commit_msg>add opt<commit_after>package database

import (
	"database/sql"
	"errors"
	"fmt"
	_ "github.com/alexbrainman/odbc"
	"github.com/blackss2/utility/convert"
	"github.com/cznic/ql"
	_ "github.com/denisenkom/go-mssqldb"
	_ "github.com/go-sql-driver/mysql"
	_ "github.com/ziutek/mymysql/godrv"
	"runtime"
	"strings"
)

type Database struct {
	inst       *sql.DB
	instQL     *ql.DB
	connString string
	driver     string
}

func (db *Database) Open(driver string, connString string) error {
	db.driver = driver
	db.connString = connString
	runtime.SetFinalizer(db, func(f interface{}) {
		f.(*Database).Close()
	})
	return db.executeOpen()
}

func (db *Database) executeOpen() error {
	var err error
	if db.driver == "ql" {
		if db.connString == "mem" {
			db.instQL, err = ql.OpenMem()
		} else {
			opt := &ql.Options{}
			opt.CanCreate = true
			db.instQL, err = ql.OpenFile(db.connString, opt)
		}
	} else {
		db.inst, err = sql.Open(db.driver, db.connString)
	}
	return err
}

func (db *Database) Close() error {
	var err error

	if db.inst != nil {
		err = db.inst.Close()
	} else if db.instQL != nil {
		err = db.instQL.Close()
	}
	return err
}

type Rows struct {
	inst    *sql.Rows
	qlRows  [][]interface{}
	qlIndex int
	isFirst bool
	isNil   bool
	Cols    []string
}

func (db *Database) prepare(queryStr string, retCount int) (*sql.Stmt, error) {
	stmt, err := db.inst.Prepare(queryStr)
	if err != nil {
		db.Close()
		db.executeOpen()
		if retCount > 0 {
			return db.prepare(queryStr, retCount-1)
		}
		return nil, err
	}
	return stmt, err
}

func (db *Database) Query(queryStr string) (*Rows, error) {
	rows := &Rows{nil, nil, 0, true, false, make([]string, 0, 100)}

	QUERYSTR := strings.ToUpper(queryStr)

	if db.inst != nil {
		stmt, err := db.prepare(queryStr, 1)
		if stmt != nil {
			defer stmt.Close()
		}
		if err != nil {
			db.Close()
			db.executeOpen()
			return db.TempQuery(queryStr)
		}
		rows.inst, err = stmt.Query()

		if err != nil {
			if err.Error() != "Stmt did not create a result set" {
				db.Close()
				db.executeOpen()
				return db.TempQuery(queryStr)
			} else {
				runtime.SetFinalizer(rows, func(f interface{}) {
					f.(*Rows).Close()
				})
				return rows, nil
			}
		}

		rows.Cols, err = rows.inst.Columns()

		if !rows.inst.Next() {
			rows.Close()
		} else {
			runtime.SetFinalizer(rows, func(f interface{}) {
				f.(*Rows).Close()
			})
		}
	} else if db.instQL != nil {
		if !strings.Contains(QUERYSTR, "TRANSACTION") && (strings.Contains(QUERYSTR, "INSERT") || strings.Contains(QUERYSTR, "CREATE") || strings.Contains(QUERYSTR, "UPDATE") || strings.Contains(QUERYSTR, "DELETE")) {
			queryStr = fmt.Sprintf(`
				BEGIN TRANSACTION;
					%s;
				COMMIT;
			`, queryStr)
		}

		ctx := ql.NewRWCtx()
		rs, _, err := db.instQL.Run(ctx, queryStr, nil)
		if err != nil {
			println("P1 : ", err.Error(), "\n", queryStr)
			return nil, err
		}

		if len(rs) == 0 {
			rows.isNil = true
			rows.isFirst = false
			return rows, nil
		}

		rows.Cols, err = rs[0].Fields()

		rows.qlRows, err = rs[0].Rows(-1, -1)
		if len(rows.qlRows) == 0 || err != nil {
			rows.Close()
		} else {
			runtime.SetFinalizer(rows, func(f interface{}) {
				f.(*Rows).Close()
			})
		}
	} else {
		return nil, errors.New("db is not initialized")
	}

	if strings.HasPrefix(QUERYSTR, "INSERT") && strings.Contains(QUERYSTR, "OUTPUT") && strings.Contains(QUERYSTR, "INSERTED.") {
		if rows.IsNil() {
			return nil, errors.New("insert.fail")
		}
	}
	return rows, nil
}

func (db *Database) TempQuery(queryStr string) (*Rows, error) {
	rows := &Rows{nil, nil, 0, true, false, make([]string, 0, 100)}

	if db.inst != nil {
		stmt, err := db.prepare(queryStr, 1)
		if stmt != nil {
			defer stmt.Close()
		}
		if err != nil {
			println("P1 : ", err.Error())
			return nil, err
		}
		rows.inst, err = stmt.Query()

		if err != nil {
			if err.Error() != "Stmt did not create a result set" {
				println("P2 : ", err.Error(), "\n", queryStr)
				return nil, err
			} else {
				runtime.SetFinalizer(rows, func(f interface{}) {
					f.(*Rows).Close()
				})
				return rows, nil
			}
		}

		rows.Cols, err = rows.inst.Columns()
		if err != nil {
			println("P2 : ", err.Error(), "\n", queryStr)
			return nil, err
		}

		if !rows.inst.Next() {
			rows.Close()
		} else {
			runtime.SetFinalizer(rows, func(f interface{}) {
				f.(*Rows).Close()
			})
		}
	} else if db.instQL != nil {
		return nil, errors.New("ql not use TempQuery")
	} else {
		return nil, errors.New("db is not initialized")
	}

	QUERYSTR := strings.ToUpper(queryStr)
	if strings.HasPrefix(QUERYSTR, "INSERT") && strings.Contains(QUERYSTR, "OUTPUT") && strings.Contains(QUERYSTR, "INSERTED.") {
		if rows.IsNil() {
			return nil, errors.New("insert.fail")
		}
	}

	return rows, nil
}

func (rows *Rows) Next() bool {
	if !rows.isNil && rows.isFirst {
		rows.isFirst = false
		return true
	}
	if rows.inst != nil {
		if !rows.inst.Next() {
			rows.Close()
		}
	} else if rows.qlRows != nil {
		rows.qlIndex++
		if len(rows.qlRows) <= rows.qlIndex {
			rows.Close()
		}
	} else {
		return false
	}
	return !rows.isNil
}

func (rows *Rows) FetchArray() []interface{} {
	if rows.isNil {
		return nil
	}
	if rows.inst != nil {
		cols, err := rows.inst.Columns()
		if err != nil {
			return nil
		}

		rawResult := make([]*interface{}, len(cols))
		result := make([]interface{}, len(cols))

		dest := make([]interface{}, len(cols))
		for i, _ := range rawResult {
			dest[i] = &rawResult[i]
		}
		rows.inst.Scan(dest...)
		for i, raw := range rawResult {
			if raw != nil {
				v := (*raw)
				switch v.(type) {
				case []byte:
					v = convert.String(v)
				}
				result[i] = v
			} else {
				result[i] = nil
			}
		}
		return result
	} else if rows.qlRows != nil {
		if len(rows.qlRows) <= rows.qlIndex {
			return nil
		}
		return rows.qlRows[rows.qlIndex]
	} else {
		return nil
	}

}

func (rows *Rows) FetchHash() map[string]interface{} {
	if rows.isNil {
		return nil
	}
	cols, err := rows.inst.Columns()
	if err != nil {
		return nil
	}

	result := make(map[string]interface{}, len(cols))

	row := rows.FetchArray()

	for i, v := range row {
		if v != nil {
			switch v.(type) {
			case []byte:
				v = convert.String(v)
			}
		}
		result[cols[i]] = v
		result[strings.ToUpper(cols[i])] = v
		result[strings.ToLower(cols[i])] = v
	}
	return result
}

func (rows *Rows) Close() error {
	if rows != nil {
		rows.isNil = true
		if rows.inst != nil {
			return rows.inst.Close()
		}
	}
	return nil
}

func (rows *Rows) IsNil() bool {
	return rows.isNil
}
<|endoftext|>


<commit_before>// Copyright (c) 2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package dcrutil

import (
	"testing"

	"github.com/decred/dcrd/chaincfg/v3"
)

func TestVerifyMessage(t *testing.T) {
	msg := "verifymessage test"

	var tests = []struct {
		addr    string
		sig     string
		params  AddressParams
		isValid bool
	}{
		// valid
		{"TsdbYVDoh3JsyP6oEg2aHVoTjsFuHzUgGKv", "IITPXfmkfLPULbX9Im3XIyHAKiXRw5N9j6P7qf0MdEP9YQinn51lWjS+8jbTceRxCWckKKssu3ZpQm1xCWKz9GA=", chaincfg.TestNet3Params(), true},

		// wrong address
		{"TsWeG3TJzucZgYyMfZFC2GhBvbeNfA48LTo", "IITPXfmkfLPULbX9Im3XIyHAKiXRw5N9j6P7qf0MdEP9YQinn51lWjS+8jbTceRxCWckKKssu3ZpQm1xCWKz9GA=", chaincfg.TestNet3Params(), false},

		// wrong signature
		{"TsdbYVDoh3JsyP6oEg2aHVoTjsFuHzUgGKv", "HxzZggzHMljSWpKHnw1Dow84KGWvTRBCG2JqBM5W4Q7iePW0dirZXCggSeXHVQ26D0MbDFffi3yw+x2Z5nQ94gg=", chaincfg.TestNet3Params(), false},

		// wrong params
		{"TsdbYVDoh3JsyP6oEg2aHVoTjsFuHzUgGKv", "IITPXfmkfLPULbX9Im3XIyHAKiXRw5N9j6P7qf0MdEP9YQinn51lWjS+8jbTceRxCWckKKssu3ZpQm1xCWKz9GA=", chaincfg.MainNetParams(), false},
	}

	for i, test := range tests {
		err := VerifyMessage(test.addr, test.sig, msg, test.params)
		if (test.isValid && err != nil) || (!test.isValid && err == nil) {
			t.Fatalf("VerifyMessage test #%d failed", i+1)
		}
	}
}
<commit_msg>dcrutil: Cleanup verify tests and use mock params.<commit_after>// Copyright (c) 2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package dcrutil

import (
	"testing"
)

// TestVerifyMessage ensures the verifying a message works as intended.
func TestVerifyMessage(t *testing.T) {
	mainNetParams := mockMainNetParams()
	testNetParams := mockTestNetParams()

	msg := "verifymessage test"

	var tests = []struct {
		name    string
		addr    string
		sig     string
		params  AddressParams
		isValid bool
	}{{
		name:    "valid",
		addr:    "TsdbYVDoh3JsyP6oEg2aHVoTjsFuHzUgGKv",
		sig:     "IITPXfmkfLPULbX9Im3XIyHAKiXRw5N9j6P7qf0MdEP9YQinn51lWjS+8jbTceRxCWckKKssu3ZpQm1xCWKz9GA=",
		params:  testNetParams,
		isValid: true,
	}, {
		name:    "wrong address",
		addr:    "TsWeG3TJzucZgYyMfZFC2GhBvbeNfA48LTo",
		sig:     "IITPXfmkfLPULbX9Im3XIyHAKiXRw5N9j6P7qf0MdEP9YQinn51lWjS+8jbTceRxCWckKKssu3ZpQm1xCWKz9GA=",
		params:  testNetParams,
		isValid: false,
	}, {
		name:    "wrong signature",
		addr:    "TsdbYVDoh3JsyP6oEg2aHVoTjsFuHzUgGKv",
		sig:     "HxzZggzHMljSWpKHnw1Dow84KGWvTRBCG2JqBM5W4Q7iePW0dirZXCggSeXHVQ26D0MbDFffi3yw+x2Z5nQ94gg=",
		params:  testNetParams,
		isValid: false,
	}, {
		name:    "wrong params",
		addr:    "TsdbYVDoh3JsyP6oEg2aHVoTjsFuHzUgGKv",
		sig:     "IITPXfmkfLPULbX9Im3XIyHAKiXRw5N9j6P7qf0MdEP9YQinn51lWjS+8jbTceRxCWckKKssu3ZpQm1xCWKz9GA=",
		params:  mainNetParams,
		isValid: false,
	}}

	for _, test := range tests {
		err := VerifyMessage(test.addr, test.sig, msg, test.params)
		if (test.isValid && err != nil) || (!test.isValid && err == nil) {
			t.Fatalf("%s: failed", test.name)
		}
	}
}
<|endoftext|>


<commit_before>package command

import (
	"strings"
	"os"
	"log"
	"fmt"
	"github.com/wantedly/developers-account-mapper/store"
	"github.com/wantedly/developers-account-mapper/models"
)

type RegisterCommand struct {
	Meta
}

func (c *RegisterCommand) Run(args []string) int {
	var loginName, githubUsername string
	if len(args) == 1 {
		loginName = os.Getenv("USER")
		githubUsername = args[0]
	} else if len(args) == 2 {
		loginName = args[0]
		githubUsername = args[1]
	} else {
		log.Println(c.Help())
		return 1
	}

	s := store.NewDynamoDB()

	user := models.NewUser(loginName, githubUsername)
	if err := s.AddUser(user); err != nil {
		log.Println(err)
		return 1
	}
	fmt.Printf("user %v added.\n", user)

	return 0
}

func (c *RegisterCommand) Synopsis() string {
	return "Register LoginName and GitHubUsername mapping"
}

func (c *RegisterCommand) Help() string {
	helpText := `

`
	return strings.TrimSpace(helpText)
}
<commit_msg>Use q instead v to render string<commit_after>package command

import (
	"strings"
	"os"
	"log"
	"fmt"
	"github.com/wantedly/developers-account-mapper/store"
	"github.com/wantedly/developers-account-mapper/models"
)

type RegisterCommand struct {
	Meta
}

func (c *RegisterCommand) Run(args []string) int {
	var loginName, githubUsername string
	if len(args) == 1 {
		loginName = os.Getenv("USER")
		githubUsername = args[0]
	} else if len(args) == 2 {
		loginName = args[0]
		githubUsername = args[1]
	} else {
		log.Println(c.Help())
		return 1
	}

	s := store.NewDynamoDB()

	user := models.NewUser(loginName, githubUsername)
	if err := s.AddUser(user); err != nil {
		log.Println(err)
		return 1
	}
	fmt.Printf("user %q added.\n", user)

	return 0
}

func (c *RegisterCommand) Synopsis() string {
	return "Register LoginName and GitHubUsername mapping"
}

func (c *RegisterCommand) Help() string {
	helpText := `

`
	return strings.TrimSpace(helpText)
}
<|endoftext|>


<commit_before>package commands

import (
	"fmt"
	"io"
	"strings"

	"github.com/cloudfoundry/bosh-bootloader/aws/cloudformation"
	"github.com/cloudfoundry/bosh-bootloader/bosh"
	"github.com/cloudfoundry/bosh-bootloader/flags"
	"github.com/cloudfoundry/bosh-bootloader/helpers"
	"github.com/cloudfoundry/bosh-bootloader/storage"
)

type Destroy struct {
	logger                  logger
	stdin                   io.Reader
	boshManager             boshManager
	vpcStatusChecker        vpcStatusChecker
	stackManager            stackManager
	infrastructureManager   infrastructureManager
	certificateDeleter      certificateDeleter
	stateStore              stateStore
	stateValidator          stateValidator
	terraformManager        terraformDestroyer
	networkInstancesChecker networkInstancesChecker
}

type destroyConfig struct {
	NoConfirm     bool
	SkipIfMissing bool
}

type vpcStatusChecker interface {
	ValidateSafeToDelete(vpcID string, envID string) error
}

type stackManager interface {
	Describe(string) (cloudformation.Stack, error)
}

type certificateDeleter interface {
	Delete(certificateName string) error
}

type stateValidator interface {
	Validate() error
}

type networkInstancesChecker interface {
	ValidateSafeToDelete(networkName string) error
}

func NewDestroy(logger logger, stdin io.Reader,
	boshManager boshManager, vpcStatusChecker vpcStatusChecker, stackManager stackManager,
	infrastructureManager infrastructureManager,
	certificateDeleter certificateDeleter, stateStore stateStore, stateValidator stateValidator,
	terraformManager terraformDestroyer, networkInstancesChecker networkInstancesChecker) Destroy {
	return Destroy{
		logger:                  logger,
		stdin:                   stdin,
		boshManager:             boshManager,
		vpcStatusChecker:        vpcStatusChecker,
		stackManager:            stackManager,
		infrastructureManager:   infrastructureManager,
		certificateDeleter:      certificateDeleter,
		stateStore:              stateStore,
		stateValidator:          stateValidator,
		terraformManager:        terraformManager,
		networkInstancesChecker: networkInstancesChecker,
	}
}

func (d Destroy) CheckFastFails(subcommandFlags []string, state storage.State) error {
	if !state.NoDirector {
		err := fastFailBOSHVersion(d.boshManager)
		if err != nil {
			return err
		}
	}

	if state.IAAS == "gcp" {
		err := d.terraformManager.ValidateVersion()
		if err != nil {
			return err
		}
	}

	config, err := d.parseFlags(subcommandFlags)
	if err != nil {
		return err
	}

	if config.SkipIfMissing && state.EnvID == "" {
		d.logger.Step("state file not found, and --skip-if-missing flag provided, exiting")
		return nil
	}

	err = d.stateValidator.Validate()
	if err != nil {
		return err
	}

	var terraformOutputs map[string]interface{}
	if state.IAAS == "gcp" {
		terraformOutputs, err = d.terraformManager.GetOutputs(state)
		if err == nil {
			networkName, ok := terraformOutputs["network_name"].(string)
			if ok {
				err = d.networkInstancesChecker.ValidateSafeToDelete(networkName)
				if err != nil {
					return err
				}
			}
		}
	}

	if state.IAAS == "aws" {
		if state.TFState != "" {
			outputs, err := d.terraformManager.GetOutputs(state)
			if err == nil {
				var vpcID = outputs["vpc_id"]
				if vpcID != nil {
					if err := d.vpcStatusChecker.ValidateSafeToDelete(vpcID.(string), state.EnvID); err != nil {
						return err
					}
				}
			}
		} else {
			stackExists := true
			var err error
			stack, err := d.stackManager.Describe(state.Stack.Name)
			switch err {
			case cloudformation.StackNotFound:
				stackExists = false
			case nil:
				break
			default:
				return err
			}

			if stackExists {
				var vpcID = stack.Outputs["VPCID"]
				if err := d.vpcStatusChecker.ValidateSafeToDelete(vpcID, ""); err != nil {
					return err
				}
			}
		}
	}

	return nil
}

func (d Destroy) Execute(subcommandFlags []string, state storage.State) error {
	config, err := d.parseFlags(subcommandFlags)
	if err != nil {
		return err
	}

	if config.SkipIfMissing && state.EnvID == "" {
		d.logger.Step("state file not found, and --skip-if-missing flag provided, exiting")
		return nil
	}

	if !config.NoConfirm {
		d.logger.Prompt(fmt.Sprintf("Are you sure you want to delete infrastructure for %q? This operation cannot be undone!", state.EnvID))

		var proceed string
		fmt.Fscanln(d.stdin, &proceed)

		proceed = strings.ToLower(proceed)
		if proceed != "yes" && proceed != "y" {
			d.logger.Step("exiting")
			return nil
		}
	}

	stack, err := d.stackManager.Describe(state.Stack.Name)
	switch err {
	case cloudformation.StackNotFound:
		break
	case nil:
		break
	default:
		return err
	}

	terraformOutputs, err := d.terraformManager.GetOutputs(state)
	if err != nil {
		return err
	}

	state, err = d.deleteBOSH(state, stack, terraformOutputs)
	switch err.(type) {
	case bosh.ManagerDeleteError:
		mdErr := err.(bosh.ManagerDeleteError)
		setErr := d.stateStore.Set(mdErr.State())
		if setErr != nil {
			errorList := helpers.Errors{}
			errorList.Add(err)
			errorList.Add(setErr)
			return errorList
		}
		return err
	case error:
		return err
	}

	if err := d.stateStore.Set(state); err != nil {
		return err
	}

	if state.IAAS == "aws" && state.TFState == "" {
		state, err = d.deleteStack(stack, state)
		if err != nil {
			return err
		}
	} else {
		state, err = d.terraformManager.Destroy(state)
		if err != nil {
			return handleTerraformError(err, d.stateStore)
		}
	}

	if err := d.stateStore.Set(state); err != nil {
		return err
	}

	if state.IAAS == "aws" {
		if state.Stack.CertificateName != "" {
			d.logger.Step("deleting certificate")
			err = d.certificateDeleter.Delete(state.Stack.CertificateName)
			if err != nil {
				return err
			}

			state.Stack.CertificateName = ""

			if err := d.stateStore.Set(state); err != nil {
				return err
			}
		}
	}

	err = d.stateStore.Set(storage.State{})
	if err != nil {
		return err
	}

	return nil
}

func (d Destroy) parseFlags(subcommandFlags []string) (destroyConfig, error) {
	destroyFlags := flags.New("destroy")

	config := destroyConfig{}
	destroyFlags.Bool(&config.NoConfirm, "n", "no-confirm", false)
	destroyFlags.Bool(&config.SkipIfMissing, "", "skip-if-missing", false)

	err := destroyFlags.Parse(subcommandFlags)
	if err != nil {
		return config, err
	}

	return config, nil
}

func (d Destroy) deleteBOSH(state storage.State, stack cloudformation.Stack, terraformOutputs map[string]interface{}) (storage.State, error) {
	if state.NoDirector {
		d.logger.Println("no BOSH director, skipping...")
		return state, nil
	}

	if !state.BOSH.IsEmpty() {
		d.logger.Step("destroying bosh director")

		err := d.boshManager.Delete(state, terraformOutputs)
		if err != nil {
			return state, err
		}

		state.BOSH = storage.BOSH{}
	}

	if !state.Jumpbox.IsEmpty() {
		d.logger.Step("destroying jumpbox")

		err := d.boshManager.DeleteJumpbox(state, terraformOutputs)
		if err != nil {
			return state, err
		}

		state.Jumpbox = storage.Jumpbox{}
	}

	return state, nil
}

func (d Destroy) deleteStack(stack cloudformation.Stack, state storage.State) (storage.State, error) {
	if state.Stack.Name == "" {
		d.logger.Println("No infrastructure found, skipping...")
		return state, nil
	}

	d.logger.Step("destroying AWS stack")
	if err := d.infrastructureManager.Delete(state.Stack.Name); err != nil {
		return state, err
	}

	state.Stack.Name = ""
	state.Stack.LBType = ""

	return state, nil
}
<commit_msg>Only a single destroy jumpbox step needed.<commit_after>package commands

import (
	"fmt"
	"io"
	"strings"

	"github.com/cloudfoundry/bosh-bootloader/aws/cloudformation"
	"github.com/cloudfoundry/bosh-bootloader/bosh"
	"github.com/cloudfoundry/bosh-bootloader/flags"
	"github.com/cloudfoundry/bosh-bootloader/helpers"
	"github.com/cloudfoundry/bosh-bootloader/storage"
)

type Destroy struct {
	logger                  logger
	stdin                   io.Reader
	boshManager             boshManager
	vpcStatusChecker        vpcStatusChecker
	stackManager            stackManager
	infrastructureManager   infrastructureManager
	certificateDeleter      certificateDeleter
	stateStore              stateStore
	stateValidator          stateValidator
	terraformManager        terraformDestroyer
	networkInstancesChecker networkInstancesChecker
}

type destroyConfig struct {
	NoConfirm     bool
	SkipIfMissing bool
}

type vpcStatusChecker interface {
	ValidateSafeToDelete(vpcID string, envID string) error
}

type stackManager interface {
	Describe(string) (cloudformation.Stack, error)
}

type certificateDeleter interface {
	Delete(certificateName string) error
}

type stateValidator interface {
	Validate() error
}

type networkInstancesChecker interface {
	ValidateSafeToDelete(networkName string) error
}

func NewDestroy(logger logger, stdin io.Reader,
	boshManager boshManager, vpcStatusChecker vpcStatusChecker, stackManager stackManager,
	infrastructureManager infrastructureManager,
	certificateDeleter certificateDeleter, stateStore stateStore, stateValidator stateValidator,
	terraformManager terraformDestroyer, networkInstancesChecker networkInstancesChecker) Destroy {
	return Destroy{
		logger:                  logger,
		stdin:                   stdin,
		boshManager:             boshManager,
		vpcStatusChecker:        vpcStatusChecker,
		stackManager:            stackManager,
		infrastructureManager:   infrastructureManager,
		certificateDeleter:      certificateDeleter,
		stateStore:              stateStore,
		stateValidator:          stateValidator,
		terraformManager:        terraformManager,
		networkInstancesChecker: networkInstancesChecker,
	}
}

func (d Destroy) CheckFastFails(subcommandFlags []string, state storage.State) error {
	if !state.NoDirector {
		err := fastFailBOSHVersion(d.boshManager)
		if err != nil {
			return err
		}
	}

	if state.IAAS == "gcp" {
		err := d.terraformManager.ValidateVersion()
		if err != nil {
			return err
		}
	}

	config, err := d.parseFlags(subcommandFlags)
	if err != nil {
		return err
	}

	if config.SkipIfMissing && state.EnvID == "" {
		d.logger.Step("state file not found, and --skip-if-missing flag provided, exiting")
		return nil
	}

	err = d.stateValidator.Validate()
	if err != nil {
		return err
	}

	var terraformOutputs map[string]interface{}
	if state.IAAS == "gcp" {
		terraformOutputs, err = d.terraformManager.GetOutputs(state)
		if err == nil {
			networkName, ok := terraformOutputs["network_name"].(string)
			if ok {
				err = d.networkInstancesChecker.ValidateSafeToDelete(networkName)
				if err != nil {
					return err
				}
			}
		}
	}

	if state.IAAS == "aws" {
		if state.TFState != "" {
			outputs, err := d.terraformManager.GetOutputs(state)
			if err == nil {
				var vpcID = outputs["vpc_id"]
				if vpcID != nil {
					if err := d.vpcStatusChecker.ValidateSafeToDelete(vpcID.(string), state.EnvID); err != nil {
						return err
					}
				}
			}
		} else {
			stackExists := true
			var err error
			stack, err := d.stackManager.Describe(state.Stack.Name)
			switch err {
			case cloudformation.StackNotFound:
				stackExists = false
			case nil:
				break
			default:
				return err
			}

			if stackExists {
				var vpcID = stack.Outputs["VPCID"]
				if err := d.vpcStatusChecker.ValidateSafeToDelete(vpcID, ""); err != nil {
					return err
				}
			}
		}
	}

	return nil
}

func (d Destroy) Execute(subcommandFlags []string, state storage.State) error {
	config, err := d.parseFlags(subcommandFlags)
	if err != nil {
		return err
	}

	if config.SkipIfMissing && state.EnvID == "" {
		d.logger.Step("state file not found, and --skip-if-missing flag provided, exiting")
		return nil
	}

	if !config.NoConfirm {
		d.logger.Prompt(fmt.Sprintf("Are you sure you want to delete infrastructure for %q? This operation cannot be undone!", state.EnvID))

		var proceed string
		fmt.Fscanln(d.stdin, &proceed)

		proceed = strings.ToLower(proceed)
		if proceed != "yes" && proceed != "y" {
			d.logger.Step("exiting")
			return nil
		}
	}

	stack, err := d.stackManager.Describe(state.Stack.Name)
	switch err {
	case cloudformation.StackNotFound:
		break
	case nil:
		break
	default:
		return err
	}

	terraformOutputs, err := d.terraformManager.GetOutputs(state)
	if err != nil {
		return err
	}

	state, err = d.deleteBOSH(state, stack, terraformOutputs)
	switch err.(type) {
	case bosh.ManagerDeleteError:
		mdErr := err.(bosh.ManagerDeleteError)
		setErr := d.stateStore.Set(mdErr.State())
		if setErr != nil {
			errorList := helpers.Errors{}
			errorList.Add(err)
			errorList.Add(setErr)
			return errorList
		}
		return err
	case error:
		return err
	}

	if err := d.stateStore.Set(state); err != nil {
		return err
	}

	if state.IAAS == "aws" && state.TFState == "" {
		state, err = d.deleteStack(stack, state)
		if err != nil {
			return err
		}
	} else {
		state, err = d.terraformManager.Destroy(state)
		if err != nil {
			return handleTerraformError(err, d.stateStore)
		}
	}

	if err := d.stateStore.Set(state); err != nil {
		return err
	}

	if state.IAAS == "aws" {
		if state.Stack.CertificateName != "" {
			d.logger.Step("deleting certificate")
			err = d.certificateDeleter.Delete(state.Stack.CertificateName)
			if err != nil {
				return err
			}

			state.Stack.CertificateName = ""

			if err := d.stateStore.Set(state); err != nil {
				return err
			}
		}
	}

	err = d.stateStore.Set(storage.State{})
	if err != nil {
		return err
	}

	return nil
}

func (d Destroy) parseFlags(subcommandFlags []string) (destroyConfig, error) {
	destroyFlags := flags.New("destroy")

	config := destroyConfig{}
	destroyFlags.Bool(&config.NoConfirm, "n", "no-confirm", false)
	destroyFlags.Bool(&config.SkipIfMissing, "", "skip-if-missing", false)

	err := destroyFlags.Parse(subcommandFlags)
	if err != nil {
		return config, err
	}

	return config, nil
}

func (d Destroy) deleteBOSH(state storage.State, stack cloudformation.Stack, terraformOutputs map[string]interface{}) (storage.State, error) {
	if state.NoDirector {
		d.logger.Println("no BOSH director, skipping...")
		return state, nil
	}

	if !state.BOSH.IsEmpty() {
		d.logger.Step("destroying bosh director")

		err := d.boshManager.Delete(state, terraformOutputs)
		if err != nil {
			return state, err
		}

		state.BOSH = storage.BOSH{}
	}

	if !state.Jumpbox.IsEmpty() {
		err := d.boshManager.DeleteJumpbox(state, terraformOutputs)
		if err != nil {
			return state, err
		}

		state.Jumpbox = storage.Jumpbox{}
	}

	return state, nil
}

func (d Destroy) deleteStack(stack cloudformation.Stack, state storage.State) (storage.State, error) {
	if state.Stack.Name == "" {
		d.logger.Println("No infrastructure found, skipping...")
		return state, nil
	}

	d.logger.Step("destroying AWS stack")
	if err := d.infrastructureManager.Delete(state.Stack.Name); err != nil {
		return state, err
	}

	state.Stack.Name = ""
	state.Stack.LBType = ""

	return state, nil
}
<|endoftext|>


<commit_before>package commands

import (
  "strings"
  "fmt"
  "github.com/mmcquillan/jane/models"
  "net/http"
  "encoding/xml"
  "io/ioutil"
  "log"
)

type Query struct {
  QueryResult string `xml:"success,attr"`
  PodList []Pod `xml:"pod"`
  Datatypes string `xml:"datatypes,attr"`
}

type Pod struct {
  Title string `xml:"title,attr"`
  SubpodList []Subpod `xml:"subpod"`
}

type Subpod struct {
  Title string `xml:"title,attr"`
  Plaintext string `xml:"plaintext"`
  Image Image `xml:"img"`
}

type Image struct {
  Source string `xml:"src,attr"`
}

var errorResult = "Error processing request"
var baseQuery = "http://api.wolframalpha.com/v2/query?appid="
var input = "&input="
var returnTypes = "&format=image,plaintext"

func Wolfram(msg string, command models.Command) (results string) {
  msg = strings.TrimSpace(strings.Replace(msg, command.Match, "", 1))
  encodedRequest := strings.Replace(msg, " ", "%20", -1)
  getRequest := baseQuery + command.ApiKey + input + encodedRequest + returnTypes

  resp, err := http.Get(getRequest)
  if err != nil {
    log.Print(err)
    return errorResult
  }

  defer resp.Body.Close()

  body, err := ioutil.ReadAll(resp.Body)

  if err != nil {
    log.Print(err)
    return errorResult
  }

  var query Query
  err = xml.Unmarshal(body, &query)
  if err != nil {
    log.Print(err)
    return errorResult
  }

  if query.QueryResult == "false" {
    return "Failed to return a result"
  }

  var result string
  if query.Datatypes == "Math" {
    result = ParseWolframAlphaMathQuery(query)
  } else {
    result = ParseWolframAlphaQuery(query)
  }

  return result
}

func ParseWolframAlphaMathQuery(query Query) string {
  result := ""

  for _, pod := range query.PodList {
    if len(pod.SubpodList) == 0 {
      continue
    }
    if pod.Title != "Result" {
      continue
    }
    for _, subpod := range pod.SubpodList {
      if subpod.Plaintext == "{}" {
        continue
      }
      result += fmt.Sprintf("     %s\n", subpod.Plaintext)
    }
  }

  return result
}

func ParseWolframAlphaQuery(query Query) string {
  result := ""

  for _, pod := range query.PodList {
    if len(pod.SubpodList) == 0 {
      continue
    }
    if pod.Title == "Input interpretation" {
      continue
    }
    for _, subpod := range pod.SubpodList {
      if subpod.Plaintext == "{}" {
        continue
      }
      result += fmt.Sprintf("     %s\n", subpod.Plaintext)
      result += fmt.Sprintf("     Image: %s\n\n", subpod.Image.Source)
    }
  }

  return result
}
<commit_msg>Removing images for now<commit_after>package commands

import (
  "strings"
  "fmt"
  "github.com/mmcquillan/jane/models"
  "net/http"
  "encoding/xml"
  "io/ioutil"
  "log"
)

type Query struct {
  QueryResult string `xml:"success,attr"`
  PodList []Pod `xml:"pod"`
  Datatypes string `xml:"datatypes,attr"`
}

type Pod struct {
  Title string `xml:"title,attr"`
  SubpodList []Subpod `xml:"subpod"`
}

type Subpod struct {
  Title string `xml:"title,attr"`
  Plaintext string `xml:"plaintext"`
  Image Image `xml:"img"`
}

type Image struct {
  Source string `xml:"src,attr"`
}

var errorResult = "Error processing request"
var baseQuery = "http://api.wolframalpha.com/v2/query?appid="
var input = "&input="
var returnTypes = "&format=image,plaintext"

func Wolfram(msg string, command models.Command) (results string) {
  msg = strings.TrimSpace(strings.Replace(msg, command.Match, "", 1))
  encodedRequest := strings.Replace(msg, " ", "%20", -1)
  getRequest := baseQuery + command.ApiKey + input + encodedRequest + returnTypes

  resp, err := http.Get(getRequest)
  if err != nil {
    log.Print(err)
    return errorResult
  }

  defer resp.Body.Close()

  body, err := ioutil.ReadAll(resp.Body)

  if err != nil {
    log.Print(err)
    return errorResult
  }

  var query Query
  err = xml.Unmarshal(body, &query)
  if err != nil {
    log.Print(err)
    return errorResult
  }

  if query.QueryResult == "false" {
    return "Failed to return a result"
  }

  var result string
  if query.Datatypes == "Math" {
    result = ParseWolframAlphaMathQuery(query)
  } else {
    result = ParseWolframAlphaQuery(query)
  }

  return result
}

func ParseWolframAlphaMathQuery(query Query) string {
  result := ""

  for _, pod := range query.PodList {
    if len(pod.SubpodList) == 0 {
      continue
    }
    if pod.Title != "Result" {
      continue
    }
    for _, subpod := range pod.SubpodList {
      if subpod.Plaintext == "{}" {
        continue
      }
      result += fmt.Sprintf("     %s\n", subpod.Plaintext)
    }
  }

  return result
}

func ParseWolframAlphaQuery(query Query) string {
  result := ""

  for _, pod := range query.PodList {
    if len(pod.SubpodList) == 0 {
      continue
    }
    if pod.Title == "Input interpretation" {
      continue
    }
    for _, subpod := range pod.SubpodList {
      if subpod.Plaintext == "{}" {
        continue
      }
      result += fmt.Sprintf("     %s\n", subpod.Plaintext)
      // result += fmt.Sprintf("     Image: %s\n\n", subpod.Image.Source)
    }
  }

  return result
}
<|endoftext|>


<commit_before>package uuid

import (
	"bytes"
	"crypto/md5"
	"crypto/rand"
	"encoding/hex"
	"v2ray.com/core/common/errors"
)

var (
	byteGroups = []int{8, 4, 4, 4, 12}
)

type UUID [16]byte

// String returns the string representation of this UUID.
func (v *UUID) String() string {
	return bytesToString(v.Bytes())
}

// Bytes returns the bytes representation of this UUID.
func (v *UUID) Bytes() []byte {
	return v[:]
}

// Equals returns true if this UUID equals another UUID by value.
func (v *UUID) Equals(another *UUID) bool {
	if v == nil && another == nil {
		return true
	}
	if v == nil || another == nil {
		return false
	}
	return bytes.Equal(v.Bytes(), another.Bytes())
}

// Next generates a deterministic random UUID based on this UUID.
func (v *UUID) Next() *UUID {
	md5hash := md5.New()
	md5hash.Write(v.Bytes())
	md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81"))
	newid := new(UUID)
	for {
		md5hash.Sum(newid[:0])
		if !newid.Equals(v) {
			return newid
		}
		md5hash.Write([]byte("533eff8a-4113-4b10-b5ce-0f5d76b98cd2"))
	}
}

func bytesToString(bytes []byte) string {
	result := hex.EncodeToString(bytes[0 : byteGroups[0]/2])
	start := byteGroups[0] / 2
	for i := 1; i < len(byteGroups); i++ {
		nBytes := byteGroups[i] / 2
		result += "-"
		result += hex.EncodeToString(bytes[start : start+nBytes])
		start += nBytes
	}
	return result
}

// New creates an UUID with random value.
func New() *UUID {
	uuid := new(UUID)
	rand.Read(uuid.Bytes())
	return uuid
}

// PraseBytes converts an UUID in byte form to object.
func ParseBytes(b []byte) (*UUID, error) {
	if len(b) != 16 {
		return nil, errors.New("Invalid UUID: ", b)
	}
	uuid := new(UUID)
	copy(uuid[:], b)
	return uuid, nil
}

// ParseString converts an UUID in string form to object.
func ParseString(str string) (*UUID, error) {
	text := []byte(str)
	if len(text) < 32 {
		return nil, errors.New("Invalid UUID: ", str)
	}

	uuid := new(UUID)
	b := uuid.Bytes()

	for _, byteGroup := range byteGroups {
		if text[0] == '-' {
			text = text[1:]
		}

		_, err := hex.Decode(b[:byteGroup/2], text[:byteGroup])

		if err != nil {
			return nil, err
		}

		text = text[byteGroup:]
		b = b[byteGroup/2:]
	}

	return uuid, nil
}
<commit_msg>refactor uuid<commit_after>package uuid

import (
	"bytes"
	"crypto/md5"
	"crypto/rand"
	"encoding/hex"

	"v2ray.com/core/common/errors"
)

var (
	byteGroups = []int{8, 4, 4, 4, 12}
)

type UUID [16]byte

// String returns the string representation of this UUID.
func (u *UUID) String() string {
	bytes := u.Bytes()
	result := hex.EncodeToString(bytes[0 : byteGroups[0]/2])
	start := byteGroups[0] / 2
	for i := 1; i < len(byteGroups); i++ {
		nBytes := byteGroups[i] / 2
		result += "-"
		result += hex.EncodeToString(bytes[start : start+nBytes])
		start += nBytes
	}
	return result
}

// Bytes returns the bytes representation of this UUID.
func (u *UUID) Bytes() []byte {
	return u[:]
}

// Equals returns true if this UUID equals another UUID by value.
func (u *UUID) Equals(another *UUID) bool {
	if u == nil && another == nil {
		return true
	}
	if u == nil || another == nil {
		return false
	}
	return bytes.Equal(u.Bytes(), another.Bytes())
}

// Next generates a deterministic random UUID based on this UUID.
func (u *UUID) Next() *UUID {
	md5hash := md5.New()
	md5hash.Write(u.Bytes())
	md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81"))
	newid := new(UUID)
	for {
		md5hash.Sum(newid[:0])
		if !newid.Equals(u) {
			return newid
		}
		md5hash.Write([]byte("533eff8a-4113-4b10-b5ce-0f5d76b98cd2"))
	}
}

// New creates an UUID with random value.
func New() *UUID {
	uuid := new(UUID)
	rand.Read(uuid.Bytes())
	return uuid
}

// ParseBytes converts an UUID in byte form to object.
func ParseBytes(b []byte) (*UUID, error) {
	if len(b) != 16 {
		return nil, errors.New("Invalid UUID: ", b)
	}
	uuid := new(UUID)
	copy(uuid[:], b)
	return uuid, nil
}

// ParseString converts an UUID in string form to object.
func ParseString(str string) (*UUID, error) {
	text := []byte(str)
	if len(text) < 32 {
		return nil, errors.New("Invalid UUID: ", str)
	}

	uuid := new(UUID)
	b := uuid.Bytes()

	for _, byteGroup := range byteGroups {
		if text[0] == '-' {
			text = text[1:]
		}

		_, err := hex.Decode(b[:byteGroup/2], text[:byteGroup])

		if err != nil {
			return nil, err
		}

		text = text[byteGroup:]
		b = b[byteGroup/2:]
	}

	return uuid, nil
}
<|endoftext|>


<commit_before>package color

type ColorNumber int

// 1 - 9
const (
	Red ColorNumber = iota + 1
	Yellow
	Green
	Cyan
	Blue
	Magenta
	White
	Grey128
	Grey192
)

// 250 - 255
const (
	Grey51 = iota + 250
	Grey91
	Grey132
	Grey173
	Grey214
	Grey255
)
<commit_msg>change ColorNumber type<commit_after>package color

type ColorNumber uint8

// 1 - 9
const (
	Red ColorNumber = iota + 1
	Yellow
	Green
	Cyan
	Blue
	Magenta
	White
	Grey128
	Grey192
)

// 250 - 255
const (
	Grey51 = iota + 250
	Grey91
	Grey132
	Grey173
	Grey214
	Grey255
)
<|endoftext|>