예제 #1
0
func main() {
	var config Config
	flag.BoolVar(&DEBUG, "debug", false, "enable debug logging")
	flag.BoolVar(&QUIET, "quiet", false, "disable output")
	flag.StringVar(&config.FieldSep, "csv-fields-terminated-by", "\t", "field separator")
	flag.StringVar(&config.RowSep, "csv-records-terminated-by", "\n", "row separator")
	flag.StringVar(&config.NullString, "csv-null-string", "\\N", "output string for NULL values")
	flag.BoolVar(&config.DateEpoch, "epoch", true, "output datetime as epoch instead of RFC3339")
	defaults_file := flag.String("defaults-file", "my.cnf", "defaults file")
	defaults_group_suffix := flag.String("defaults-group-suffix", "", "defaults group suffix")
	format := flag.String("format", "json", "output format 'json' or 'csv'")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: mysqlcsvdump [options] database table > output.json\n\n")
		fmt.Fprintf(os.Stderr, "Reads connection info from ./my.cnf. Use '-' for table to send query in stdin\n\n")
		flag.PrintDefaults()
	}
	flag.Parse()

	args := flag.Args()
	if len(args) < 2 {
		flag.Usage()
		os.Exit(1)
	}
	dsn := getDSN(*defaults_file, "client"+*defaults_group_suffix, args[0])
	rows := getRows(dsn, args[1])
	if *format == "json" {
		NewJsonWriter(&config).WriteRows(rows)
	} else {
		NewCsvWriter(&config).WriteRows(rows)
	}
}
예제 #2
0
// main takes care of how to run when called via the CLI
func main() {
	// Add MongoDB flags to the parser
	flag.StringVar(&conf.Config.MongoURI, "MongoURI", conf.Config.MongoURI, "MongoDB server URI")
	flag.StringVar(&conf.Config.MongoDatabase, "MongoDatabase", conf.Config.MongoDatabase, "MongoDB database")

	// Make a new server.
	server := goserv.NewServer(&conf.Config.BaseConfiguration)
	wsContainer := restful.NewContainer()
	p := api.Product{}
	p.Register(wsContainer)

	status := api.StatusResource{}
	status.Register(wsContainer)

	groups := api.GroupsResource{}
	groups.Register(wsContainer)

	hash := api.HashResource{}
	hash.Register(wsContainer)

	wsContainer.ServiceErrorHandler(api.ErrorHandler)

	// Enable swagger
	/*config := swagger.Config{
		WebServices: wsContainer.RegisteredWebServices(),
		ApiPath:     "/service/v3/apidocs.json",
	}
	swagger.RegisterSwaggerService(config, wsContainer)
	*/
	// Set the handler to our gorilla mux router wrapped with LogAccess
	server.Handler = goserv.LogAccess(wsContainer)
	goserv.Logger.Fatal(server.ListenAndServe())
}
예제 #3
0
func (cm *ConfigManager) parseFlags() {
	pflag.StringVarP(&cm.Flags.ConfigFile, "configfile", "c", DefaultConfigFile, "Path to config")
	pflag.StringVarP(&cm.Flags.DestHost, "dest-host", "d", "", "Destination syslog hostname or IP")
	pflag.IntVarP(&cm.Flags.DestPort, "dest-port", "p", 0, "Destination syslog port")
	if utils.CanDaemonize {
		pflag.BoolVarP(&cm.Flags.NoDaemonize, "no-detach", "D", false, "Don't daemonize and detach from the terminal")
	} else {
		cm.Flags.NoDaemonize = true
	}
	pflag.StringVarP(&cm.Flags.Facility, "facility", "f", "user", "Facility")
	pflag.StringVar(&cm.Flags.Hostname, "hostname", "", "Local hostname to send from")
	pflag.StringVar(&cm.Flags.PidFile, "pid-file", "", "Location of the PID file")
	// --parse-syslog
	pflag.StringVarP(&cm.Flags.Severity, "severity", "s", "notice", "Severity")
	// --strip-color
	pflag.BoolVar(&cm.Flags.UseTCP, "tcp", false, "Connect via TCP (no TLS)")
	pflag.BoolVar(&cm.Flags.UseTLS, "tls", false, "Connect via TCP with TLS")
	pflag.BoolVar(&cm.Flags.Poll, "poll", false, "Detect changes by polling instead of inotify")
	pflag.Var(&cm.Flags.RefreshInterval, "new-file-check-interval", "How often to check for new files")
	_ = pflag.Bool("no-eventmachine-tail", false, "No action, provided for backwards compatibility")
	_ = pflag.Bool("eventmachine-tail", false, "No action, provided for backwards compatibility")
	pflag.StringVar(&cm.Flags.DebugLogFile, "debug-log-cfg", "", "the debug log file")
	pflag.StringVar(&cm.Flags.LogLevels, "log", "<root>=INFO", "\"logging configuration <root>=INFO;first=TRACE\"")
	pflag.Parse()
	cm.FlagFiles = pflag.Args()
}
예제 #4
0
파일: main.go 프로젝트: heroku/busl
func parseFlags() (*cmdConfig, *busltee.Config, error) {
	publisherConf := &busltee.Config{}
	cmdConf := &cmdConfig{}

	cmdConf.RollbarEnvironment = os.Getenv("ROLLBAR_ENVIRONMENT")
	cmdConf.RollbarToken = os.Getenv("ROLLBAR_TOKEN")

	// Connection related flags
	flag.BoolVarP(&publisherConf.Insecure, "insecure", "k", false, "allows insecure SSL connections")
	flag.IntVar(&publisherConf.Retry, "retry", 5, "max retries for connect timeout errors")
	flag.IntVar(&publisherConf.StreamRetry, "stream-retry", 60, "max retries for streamer disconnections")
	flag.Float64Var(&publisherConf.Timeout, "connect-timeout", 1, "max number of seconds to connect to busl URL")

	// Logging related flags
	flag.StringVar(&publisherConf.LogPrefix, "log-prefix", "", "log prefix")
	flag.StringVar(&publisherConf.LogFile, "log-file", "", "log file")
	flag.StringVar(&publisherConf.RequestID, "request-id", "", "request id")

	if flag.Parse(); len(flag.Args()) < 2 {
		return nil, nil, errors.New("insufficient args")
	}

	publisherConf.URL = flag.Arg(0)
	publisherConf.Args = flag.Args()[1:]

	return cmdConf, publisherConf, nil
}
예제 #5
0
파일: main.go 프로젝트: ashcrow/victims-api
// main takes care of how to run when called via the CLI
func main() {
	// Add MongoDB flags to the parser
	flag.StringVar(&conf.Config.MongoURI, "MongoURI", conf.Config.MongoURI, "MongoDB server URI")
	flag.StringVar(&conf.Config.MongoDatabase, "MongoDatabase", conf.Config.MongoDatabase, "MongoDB database")

	// Make a new server.
	server := goserv.NewServer(&conf.Config.BaseConfiguration)
	wsContainer := restful.NewContainer()
	p := api.Product{}
	p.Register(wsContainer)

	status := api.StatusResource{}
	status.Register(wsContainer)

	groups := api.GroupsResource{}
	groups.Register(wsContainer)

	hash := api.HashResource{}
	hash.Register(wsContainer)

	coordinates := api.CoordinatesResource{}
	coordinates.Register(wsContainer)

	wsContainer.ServiceErrorHandler(api.ErrorHandler)

	// Set the handler to our gorilla mux router wrapped with LogAccess
	server.Handler = goserv.LogAccess(wsContainer)
	goserv.Logger.Fatal(server.ListenAndServe())
}
예제 #6
0
func Go() {
	smtpAddr := "localhost:1025"

	goflag := false
	for _, g := range os.Args[1:] {
		if strings.HasPrefix(g, "-") && !strings.HasPrefix(g, "--") {
			if strings.HasPrefix(g, "-from ") || strings.HasPrefix(g, "-from=") ||
				strings.HasPrefix(g, "-smtp-addr ") || strings.HasPrefix(g, "-smtp-addr=") {
				goflag = true
				break
			}
		}
	}

	host, err := os.Hostname()
	if err != nil {
		host = "localhost"
	}

	username := "******"
	user, err := user.Current()
	if err == nil && user != nil && len(user.Username) > 0 {
		username = user.Username
	}

	fromAddr := username + "@" + host
	var recip []string

	if goflag {
		flag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
		flag.StringVar(&fromAddr, "from", fromAddr, "SMTP sender")

		flag.Parse()
		recip = flag.Args()
	} else {
		pflag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
		pflag.StringVarP(&fromAddr, "from", "f", fromAddr, "SMTP sender")

		pflag.Parse()
		recip = pflag.Args()
	}

	if len(recip) == 0 {
		fmt.Fprintln(os.Stderr, "missing recipient")
		os.Exit(10)
	}

	body, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error reading stdin")
		os.Exit(11)
	}

	err = smtp.SendMail(smtpAddr, nil, fromAddr, recip, body)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error sending mail")
		log.Fatal(err)
	}

}
예제 #7
0
파일: main.go 프로젝트: dawanda/mmsd
func (mmsd *mmsdService) Run() {
	flag.BoolVarP(&mmsd.Verbose, "verbose", "v", mmsd.Verbose, "Set verbosity level")
	flag.IPVar(&mmsd.MarathonIP, "marathon-ip", mmsd.MarathonIP, "Marathon endpoint TCP IP address")
	flag.UintVar(&mmsd.MarathonPort, "marathon-port", mmsd.MarathonPort, "Marathon endpoint TCP port number")
	flag.DurationVar(&mmsd.ReconnectDelay, "reconnect-delay", mmsd.ReconnectDelay, "Marathon reconnect delay")
	flag.StringVar(&mmsd.RunStateDir, "run-state-dir", mmsd.RunStateDir, "Path to directory to keep run-state")
	flag.StringVar(&mmsd.FilterGroups, "filter-groups", mmsd.FilterGroups, "Application group filter")
	flag.IPVar(&mmsd.ManagedIP, "managed-ip", mmsd.ManagedIP, "IP-address to manage for mmsd")
	flag.BoolVar(&mmsd.GatewayEnabled, "enable-gateway", mmsd.GatewayEnabled, "Enables gateway support")
	flag.IPVar(&mmsd.GatewayAddr, "gateway-bind", mmsd.GatewayAddr, "gateway bind address")
	flag.UintVar(&mmsd.GatewayPortHTTP, "gateway-port-http", mmsd.GatewayPortHTTP, "gateway port for HTTP")
	flag.UintVar(&mmsd.GatewayPortHTTPS, "gateway-port-https", mmsd.GatewayPortHTTPS, "gateway port for HTTPS")
	flag.BoolVar(&mmsd.FilesEnabled, "enable-files", mmsd.FilesEnabled, "enables file based service discovery")
	flag.BoolVar(&mmsd.UDPEnabled, "enable-udp", mmsd.UDPEnabled, "enables UDP load balancing")
	flag.BoolVar(&mmsd.TCPEnabled, "enable-tcp", mmsd.TCPEnabled, "enables haproxy TCP load balancing")
	flag.BoolVar(&mmsd.LocalHealthChecks, "enable-health-checks", mmsd.LocalHealthChecks, "Enable local health checks (if available) instead of relying on Marathon health checks alone.")
	flag.StringVar(&mmsd.HaproxyBin, "haproxy-bin", mmsd.HaproxyBin, "path to haproxy binary")
	flag.StringVar(&mmsd.HaproxyTailCfg, "haproxy-cfgtail", mmsd.HaproxyTailCfg, "path to haproxy tail config file")
	flag.IPVar(&mmsd.ServiceAddr, "haproxy-bind", mmsd.ServiceAddr, "haproxy management port")
	flag.UintVar(&mmsd.HaproxyPort, "haproxy-port", mmsd.HaproxyPort, "haproxy management port")
	flag.BoolVar(&mmsd.DnsEnabled, "enable-dns", mmsd.DnsEnabled, "Enables DNS-based service discovery")
	flag.UintVar(&mmsd.DnsPort, "dns-port", mmsd.DnsPort, "DNS service discovery port")
	flag.BoolVar(&mmsd.DnsPushSRV, "dns-push-srv", mmsd.DnsPushSRV, "DNS service discovery to also push SRV on A")
	flag.StringVar(&mmsd.DnsBaseName, "dns-basename", mmsd.DnsBaseName, "DNS service discovery's base name")
	flag.DurationVar(&mmsd.DnsTTL, "dns-ttl", mmsd.DnsTTL, "DNS service discovery's reply message TTL")
	showVersionAndExit := flag.BoolP("version", "V", false, "Shows version and exits")

	flag.Usage = func() {
		showVersion()
		fmt.Fprintf(os.Stderr, "\nUsage: mmsd [flags ...]\n\n")
		flag.PrintDefaults()
		fmt.Fprintf(os.Stderr, "\n")
	}

	flag.Parse()

	if *showVersionAndExit {
		showVersion()
		os.Exit(0)
	}

	mmsd.setupHandlers()
	mmsd.setupEventBusListener()
	mmsd.setupHttpService()

	<-mmsd.quitChannel
}
예제 #8
0
// NewServer creates a new http.Server instance based off the BaseConfiguration.
// NewServer also handles reading the TOML configuration file and
// providing/reading the command line flags. Because of this
// NewServer should always be called after all flags have been defined.
func NewServer(conf *BaseConfiguration) http.Server {
	// TOML configuration file can overwrite defaults
	tomlData, err := ioutil.ReadFile(os.Args[len(os.Args)-1])
	if err != nil {
		defer Logger.Info("No conf. Skipping.")
	} else {
		if _, err := toml.Decode(string(tomlData), &conf); err != nil {
			defer Logger.Errorf("Configuration file could not be decoded. %s. Exiting...", err)
		}
	}
	// Flags can override config items
	// Server flags
	flag.StringVar(&conf.BindAddress, "BindAddress", conf.BindAddress, "Bind address.")
	flag.IntVar(&conf.BindPort, "BindPort", conf.BindPort, "HTTP bind port.")
	flag.DurationVar(&conf.ReadTimeout, "ReadTimeout", conf.ReadTimeout, "Read timeout.")
	flag.DurationVar(&conf.WriteTimeout, "WriteTimeout", conf.WriteTimeout, "Write timeout.")
	flag.IntVar(&conf.MaxHeaderBytes, "MaxHeaderBytes", conf.MaxHeaderBytes, "Max header bytes.")

	// Server Logger flags
	flag.StringVar(&conf.LogLevel, "LogLevel", conf.LogLevel, "Log level.")
	flag.StringVar(&conf.LogFile, "LogFile", conf.LogFile, "Log file.")

	// TLS related flags
	flag.IntVar(&conf.BindHttpsPort, "BindHttpsPort", conf.BindHttpsPort, "HTTPS bind port.")
	flag.StringVar(&conf.CertFile, "CertFile", conf.CertFile, "Cert file.")
	flag.StringVar(&conf.KeyFile, "KeyFile", conf.KeyFile, "Key file.")
	flag.Parse()

	// Logging specific work also injecting the logrus log into the Servers errorlog
	// BUG(ashcrow): This needs work!!!
	makeLogger(conf)
	Logger.Debugf("Final configuration: %+v", conf)
	w := Logger.Writer()
	defer w.Close()
	ServerErrorLogger = *log.New(w, "ServerErrorLogger", log.Lshortfile)
	// -------------

	// Return the configured http.Server
	return http.Server{
		Addr:           fmt.Sprintf("%s:%d", conf.BindAddress, conf.BindPort),
		ReadTimeout:    conf.ReadTimeout,
		WriteTimeout:   conf.WriteTimeout,
		MaxHeaderBytes: conf.MaxHeaderBytes,
		ErrorLog:       &ServerErrorLogger,
	}
}
예제 #9
0
파일: main.go 프로젝트: andrew-d/sbuild
func init() {
	flag.StringVarP(&flagPlatform, "platform", "p", "linux",
		"the platform to build for")
	flag.StringVarP(&flagArch, "arch", "a", "amd64",
		"the architecture to build for")
	flag.StringVar(&flagBuildDir, "build-dir", "/tmp/sbuild",
		"the directory to use as a build directory")
	flag.BoolVarP(&flagVerbose, "verbose", "v", false, "be verbose")
}
예제 #10
0
파일: main.go 프로젝트: sguzwf/go-wiki
func main() {
	flag.Usage = func() {
		fmt.Fprint(os.Stderr, Usage)
	}

	flag.IntVarP(&options.Port, "port", "p", 8080, "")
	flag.StringVar(&options.CustomCSS, "custom-css", "", "")

	flag.Parse()

	options.Dir = flag.Arg(0)

	if options.Dir == "" {
		flag.Usage()
		os.Exit(1)
	}

	log.Println("Serving wiki from", options.Dir)

	// Parse base template
	var err error
	options.template, err = template.New("base").Parse(Template)
	if err != nil {
		log.Fatalln("Error parsing HTML template:", err)
	}

	// Trim trailing slash from root path
	if strings.HasSuffix(options.Dir, "/") {
		options.Dir = options.Dir[:len(options.Dir)-1]
	}

	// Verify that the wiki folder exists
	_, err = os.Stat(options.Dir)
	if os.IsNotExist(err) {
		log.Fatalln("Directory not found")
	}

	// Check if the wiki folder is a Git repository
	options.git = IsGitRepository(options.Dir)
	if options.git {
		log.Println("Git repository found in directory")
	} else {
		log.Println("No git repository found in directory")
	}

	http.Handle("/api/diff/", commonHandler(DiffHandler))
	http.Handle("/", commonHandler(WikiHandler))

	log.Println("Listening on:", options.Port)
	http.ListenAndServe(fmt.Sprintf(":%d", options.Port), nil)
}
예제 #11
0
파일: main.go 프로젝트: bmbernie/socks
func init() {
	flag.BoolVarP(&flagVerbose, "verbose", "v", false, "be more verbose")
	flag.BoolVarP(&flagQuiet, "quiet", "q", false, "be quiet")
	flag.BoolVarP(&flagTrace, "trace", "t", false, "trace bytes copied")

	flag.StringVarP(&flagHost, "host", "h", "", "host to listen on")
	flag.Uint16VarP(&flagPort, "port", "p", 8000, "port to listen on")
	flag.VarP(&flagAllowedSourceIPs, "source-ips", "s",
		"valid source IP addresses (if none given, all allowed)")
	flag.VarP(&flagAllowedDestinationIPs, "dest-ips", "d",
		"valid destination IP addresses (if none given, all allowed)")

	flag.StringVar(&flagRemoteListener, "remote-listener", "",
		"open the SOCKS port on the remote address (e.g. ssh://user:pass@host:port)")
}
예제 #12
0
func init() {
	pflag.DurationVar(&config.TickerTime, "ticker-time", 60*time.Second, "Ticker time.")
	pflag.DurationVar(&config.DeleteTime, "delete-time", 60*time.Minute, "Time before deleting undesired units.")
	pflag.DurationVar(&config.UpdateCooldownTime, "update-cooldown-time", 15*time.Minute, "Time between updates of changed units.")

	pflag.StringVar(&config.MachineTag, "machine-tag", "", "The machine-tag to filter for.")
	pflag.StringVar(&config.UnitTemplate, "unit-template", "", "The template to render for new units. Prefix with @ to load from a file.")
	pflag.StringVar(&config.UnitPrefix, "unit-prefix", "", "The prefix for the units to identify.")

	pflag.StringVar(&glogFlags.logToStderr, "logtostderr", "true", "log to standard error instead of files")
	pflag.StringVar(&glogFlags.alsoLogToStderr, "alsologtostderr", "false", "log to standard error as well as files")
	pflag.StringVarP(&glogFlags.verbosity, "verbose", "v", "1", "log level for V logs")
	pflag.StringVar(&glogFlags.vmodule, "vmodule", "", "comma-separated list of pattern=N settings for file-filtered logging")
	pflag.StringVar(&glogFlags.logBacktraceAt, "log_backtrace_at", "", "when logging hits line file:N, emit a stack trace")
}
예제 #13
0
func init() {
	pflag.DurationVar(&config.TickerTime, "ticker-time", 15*time.Minute, "Ticker time.")
	pflag.BoolVar(&config.DryRun, "dry-run", true, "Write to STDOUT instead of InfluxDB")
	pflag.IntVar(&config.OWMcityID, "owm-city-id", 0, "Open Weather Map city ID")

	pflag.IntVar(&config.InfluxPort, "influx-port", 8086, "InfluxDB Port")
	pflag.StringVar(&config.InfluxHost, "influx-host", "localhost", "InfluxDB Port")
	pflag.StringVar(&config.InfluxUser, "influx-user", "", "InfluxDB User")
	pflag.StringVar(&config.InfluxDB, "influx-db", "", "InfluxDB Database")
	pflag.StringVar(&config.InfluxPassword, "influx-password", "", "InfluxDB Password")
	pflag.StringVar(&config.InfluxRetention, "influx-retention", "default", "InfluxDB Retention")

	pflag.StringVar(&config.DhtType, "dht-type", "DHT22", "DHT Type (DHT11, DHT22)")
	pflag.IntVar(&config.DhtPin, "dht-pin", 4, "Pin Number DHT Data is connected to")
	pflag.BoolVar(&config.DHTPerf, "dht-perf", false, "Run DHT read in Boost Performance Mode - true will result in needing sudo")
	pflag.IntVar(&config.DhtRetries, "dht-retries", 15, "Number of reading data retries")

}
예제 #14
0
파일: cmd.go 프로젝트: iefbr14/mhsendmail
// Go runs the MailHog sendmail replacement.
func Go() {
	smtpAddr := "localhost:1025"

	goflag := false
	for _, g := range os.Args[1:] {
		if strings.HasPrefix(g, "-") && !strings.HasPrefix(g, "--") {
			if strings.HasPrefix(g, "-from ") || strings.HasPrefix(g, "-from=") ||
				strings.HasPrefix(g, "-smtp-addr ") || strings.HasPrefix(g, "-smtp-addr=") {
				goflag = true
				break
			}
		}
	}

	host, err := os.Hostname()
	if err != nil {
		host = "localhost"
	}

	username := "******"
	user, err := user.Current()
	if err == nil && user != nil && len(user.Username) > 0 {
		username = user.Username
	}

	fromAddr := username + "@" + host
	var recip []string

	if goflag {
		flag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
		flag.StringVar(&fromAddr, "from", fromAddr, "SMTP sender")

		flag.Parse()
		recip = flag.Args()
	} else {
		pflag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
		pflag.StringVarP(&fromAddr, "from", "f", fromAddr, "SMTP sender")

		pflag.Parse()
		recip = pflag.Args()
	}

	body, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error reading stdin")
		os.Exit(11)
	}

	if len(recip) == 0 {
		// We only need to parse the message to get a recipient if none where
		// provided on the command line.
		//		re := regexp.MustCompile("(?im)^To: (.*)\r*\n$")
		re := regexp.MustCompile("(?im)^To: (.*)\r*$")
		n := bytes.IndexByte(body, 0)
		var bodyStr string
		if n < 0 {
			bodyStr = string(body)
		} else {
			bodyStr = string(body[:n])
		}
		includedRecip := re.FindAllString(bodyStr, -1)
		if includedRecip == nil {
			fmt.Fprintln(os.Stderr, "missing recipient")
			os.Exit(10)
		}
		newRecip := make([]string, len(recip), len(recip)+len(includedRecip))
		copy(newRecip, recip)
		recip = append(newRecip, includedRecip...)
	}

	err = smtp.SendMail(smtpAddr, nil, fromAddr, recip, body)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error sending mail")
		log.Fatal(err)
	}

}
예제 #15
0
// Read configuration from both profile and flags. Flags override profile.
func config() error {
	var err error
	if B2D.Dir, err = getCfgDir(".boot2docker"); err != nil {
		return fmt.Errorf("failed to get current directory: %s", err)
	}

	filename := os.Getenv("BOOT2DOCKER_PROFILE")
	if filename == "" {
		filename = filepath.Join(B2D.Dir, "profile")
	}
	profile, err := getProfile(filename)
	if err != nil && !os.IsNotExist(err) { // undefined/empty profile works
		return err
	}

	if p := os.Getenv("VBOX_INSTALL_PATH"); p != "" && runtime.GOOS == "windows" {
		B2D.VBM = profile.Get("", "vbm", filepath.Join(p, "VBoxManage.exe"))
	} else {
		B2D.VBM = profile.Get("", "vbm", "VBoxManage")
	}

	B2D.SSH = profile.Get("", "ssh", "ssh")
	B2D.VM = profile.Get("", "vm", "boot2docker-vm")
	B2D.ISO = profile.Get("", "iso", filepath.Join(B2D.Dir, "boot2docker.iso"))

	if diskSize, err := strconv.ParseUint(profile.Get("", "disksize", "20000"), 10, 32); err != nil {
		return fmt.Errorf("invalid disk image size: %s", err)
	} else {
		B2D.DiskSize = uint(diskSize)
	}

	if memory, err := strconv.ParseUint(profile.Get("", "memory", "1024"), 10, 32); err != nil {
		return fmt.Errorf("invalid memory size: %s", err)
	} else {
		B2D.Memory = uint(memory)
	}

	if sshPort, err := strconv.ParseUint(profile.Get("", "sshport", "2022"), 10, 16); err != nil {
		return fmt.Errorf("invalid SSH port: %s", err)
	} else {
		B2D.SSHPort = uint16(sshPort)
	}

	if dockerPort, err := strconv.ParseUint(profile.Get("", "dockerport", "4243"), 10, 16); err != nil {
		return fmt.Errorf("invalid DockerPort: %s", err)
	} else {
		B2D.DockerPort = uint16(dockerPort)
	}

	// Host only networking settings
	B2D.HostIP = profile.Get("", "hostiP", "192.168.59.3")
	B2D.DHCPIP = profile.Get("", "dhcpip", "192.168.59.99")
	B2D.NetworkMask = profile.Get("", "netmask", "255.255.255.0")
	B2D.LowerIPAddress = profile.Get("", "lowerip", "192.168.59.103")
	B2D.UpperIPAddress = profile.Get("", "upperip", "192.168.59.254")
	B2D.DHCPEnabled = profile.Get("", "dhcp", "Yes")

	// Commandline flags override profile settings.
	flag.StringVar(&B2D.VBM, "vbm", B2D.VBM, "Path to VirtualBox management utility")
	flag.StringVar(&B2D.SSH, "ssh", B2D.SSH, "Path to SSH client utility")
	flag.StringVarP(&B2D.Dir, "dir", "d", B2D.Dir, "boot2docker config directory")
	flag.StringVar(&B2D.ISO, "iso", B2D.ISO, "Path to boot2docker ISO image")
	flag.UintVarP(&B2D.DiskSize, "disksize", "s", B2D.DiskSize, "boot2docker disk image size (in MB)")
	flag.UintVarP(&B2D.Memory, "memory", "m", B2D.Memory, "Virtual machine memory size (in MB)")
	flag.Var(newUint16Value(B2D.SSHPort, &B2D.SSHPort), "sshport", "Host SSH port (forward to port 22 in VM)")
	flag.Var(newUint16Value(B2D.DockerPort, &B2D.DockerPort), "dockerport", "Host Docker port (forward to port 4243 in VM)")
	flag.StringVar(&B2D.HostIP, "hostip", B2D.HostIP, "VirtualBox host-only network IP address")
	flag.StringVar(&B2D.NetworkMask, "netmask", B2D.NetworkMask, "VirtualBox host-only network mask")
	flag.StringVar(&B2D.DHCPEnabled, "dhcp", B2D.DHCPEnabled, "Enable VirtualBox host-only network DHCP")
	flag.StringVar(&B2D.DHCPIP, "dhcpip", B2D.DHCPIP, "VirtualBox host-only network DHCP server address")
	flag.StringVar(&B2D.LowerIPAddress, "lowerip", B2D.LowerIPAddress, "VirtualBox host-only network DHCP lower bound")
	flag.StringVar(&B2D.UpperIPAddress, "upperip", B2D.UpperIPAddress, "VirtualBox host-only network DHCP upper bound")

	flag.Parse()

	// Name of VM is the second argument.
	if vm := flag.Arg(1); vm != "" {
		B2D.VM = vm
	}
	return nil
}
예제 #16
0
func (config *Config) parseFlags() {
	// registry
	flag.StringVar(&config.Registry.Auth, "registry-auth", "", "Registry basic auth")
	flag.StringVar(&config.Registry.Datacenter, "registry-datacenter", "", "Registry datacenter")
	flag.StringVar(&config.Registry.Location, "registry", "http://localhost:8500", "Registry location")
	flag.StringVar(&config.Registry.Token, "registry-token", "", "Registry ACL token")
	flag.BoolVar(&config.Registry.NoVerifySSL, "registry-noverify", false, "don't verify registry SSL certificates")
	flag.StringVar(&config.Registry.Prefix, "registry-prefix", "marathon", "prefix for all values sent to the registry")

	// Web
	flag.StringVar(&config.Web.Listen, "listen", ":4000", "accept connections at this address")

	// Marathon
	flag.StringVar(&config.Marathon.Location, "marathon-location", "localhost:8080", "marathon URL")
	flag.StringVar(&config.Marathon.Protocol, "marathon-protocol", "http", "marathon protocol (http or https)")
	flag.StringVar(&config.Marathon.Username, "marathon-username", "", "marathon username for basic auth")
	flag.StringVar(&config.Marathon.Password, "marathon-password", "", "marathon password for basic auth")

	// General
	flag.StringVar(&config.LogLevel, "log-level", "info", "log level: panic, fatal, error, warn, info, or debug")

	flag.Parse()
}
예제 #17
0
파일: main.go 프로젝트: SpectoLabs/hoverfly
func main() {
	output := ""
	mimetype := ""
	filetype := ""
	match := ""
	siteurl := ""

	htmlMinifier := &html.Minifier{}
	xmlMinifier := &xml.Minifier{}

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [options] [input]\n\nOptions:\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Fprintf(os.Stderr, "\nInput:\n  Files or directories, leave blank to use stdin\n")
	}
	flag.StringVarP(&output, "output", "o", "", "Output file or directory, leave blank to use stdout")
	flag.StringVar(&mimetype, "mime", "", "Mimetype (text/css, application/javascript, ...), optional for input filenames, has precendence over -type")
	flag.StringVar(&filetype, "type", "", "Filetype (css, html, js, ...), optional for input filenames")
	flag.StringVar(&match, "match", "", "Filename pattern matching using regular expressions, see https://github.com/google/re2/wiki/Syntax")
	flag.BoolVarP(&recursive, "recursive", "r", false, "Recursively minify directories")
	flag.BoolVarP(&hidden, "all", "a", false, "Minify all files, including hidden files and files in hidden directories")
	flag.BoolVarP(&list, "list", "l", false, "List all accepted filetypes")
	flag.BoolVarP(&verbose, "verbose", "v", false, "Verbose")
	flag.BoolVarP(&watch, "watch", "w", false, "Watch files and minify upon changes")
	flag.BoolVarP(&update, "update", "u", false, "Update binary")

	flag.StringVar(&siteurl, "url", "", "URL of file to enable URL minification")
	flag.BoolVar(&htmlMinifier.KeepDefaultAttrVals, "html-keep-default-attrvals", false, "Preserve default attribute values")
	flag.BoolVar(&htmlMinifier.KeepWhitespace, "html-keep-whitespace", false, "Preserve whitespace characters but still collapse multiple whitespace into one")
	flag.BoolVar(&xmlMinifier.KeepWhitespace, "xml-keep-whitespace", false, "Preserve whitespace characters but still collapse multiple whitespace into one")
	flag.Parse()
	rawInputs := flag.Args()

	Error = log.New(os.Stderr, "ERROR: ", 0)
	if verbose {
		Info = log.New(os.Stderr, "INFO: ", 0)
	} else {
		Info = log.New(ioutil.Discard, "INFO: ", 0)
	}

	if update {
		if err := equinoxUpdate(); err != nil {
			Error.Fatalln(err)
		}
		return
	}

	if list {
		var keys []string
		for k := range filetypeMime {
			keys = append(keys, k)
		}
		sort.Strings(keys)
		for _, k := range keys {
			fmt.Println(k + "\t" + filetypeMime[k])
		}
		return
	}

	usePipe := len(rawInputs) == 0
	mimetype = getMimetype(mimetype, filetype, usePipe)

	var err error
	if match != "" {
		pattern, err = regexp.Compile(match)
		if err != nil {
			Error.Fatalln(err)
		}
	}

	tasks, dirDst, ok := expandInputs(rawInputs)
	if !ok {
		os.Exit(1)
	}

	if output != "" {
		output = sanitizePath(output)
		if dirDst {
			if output[len(output)-1] != '/' {
				output += "/"
			}
			if err := os.MkdirAll(output, 0777); err != nil {
				Error.Fatalln(err)
			}
		}
	}

	if ok = expandOutputs(output, usePipe, &tasks); !ok {
		os.Exit(1)
	}

	m = min.New()
	m.AddFunc("text/css", css.Minify)
	m.Add("text/html", htmlMinifier)
	m.AddFunc("text/javascript", js.Minify)
	m.AddFunc("image/svg+xml", svg.Minify)
	m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
	m.AddRegexp(regexp.MustCompile("[/+]xml$"), xmlMinifier)

	if m.URL, err = url.Parse(siteurl); err != nil {
		Error.Fatalln(err)
	}

	var watcher *RecursiveWatcher
	if watch {
		if usePipe || output == "" {
			Error.Fatalln("watch only works with files that do not overwrite themselves")
		} else if len(rawInputs) > 1 {
			Error.Fatalln("watch only works with one input directory")
		}

		input := sanitizePath(rawInputs[0])
		info, err := os.Stat(input)
		if err != nil || !info.Mode().IsDir() {
			Error.Fatalln("watch only works with an input directory")
		}

		watcher, err = NewRecursiveWatcher(input, recursive)
		if err != nil {
			Error.Fatalln(err)
		}
		defer watcher.Close()
	}

	start := time.Now()

	var fails int32
	if verbose {
		for _, t := range tasks {
			if ok := minify(mimetype, t); !ok {
				fails++
			}
		}
	} else {
		var wg sync.WaitGroup
		for _, t := range tasks {
			wg.Add(1)
			go func(t task) {
				defer wg.Done()
				if ok := minify(mimetype, t); !ok {
					atomic.AddInt32(&fails, 1)
				}
			}(t)
		}
		wg.Wait()
	}

	if watch {
		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Interrupt)

		input := sanitizePath(rawInputs[0])
		files := watcher.Run()
		for files != nil {
			select {
			case <-c:
				watcher.Close()
			case file, ok := <-files:
				if !ok {
					files = nil
					break
				}
				file = sanitizePath(file)
				if strings.HasPrefix(file, input) {
					t := task{src: file, srcDir: input}
					if t.dst, err = getOutputFilename(output, t); err != nil {
						Error.Println(err)
						continue
					}
					if !verbose {
						fmt.Fprintln(os.Stderr, file, "changed")
					}
					if ok := minify(mimetype, t); !ok {
						fails++
					}
				}
			}
		}
	}

	if verbose {
		Info.Println(time.Since(start), "total")
	}

	if fails > 0 {
		os.Exit(1)
	}
}
예제 #18
0
func readConfig(parse bool) {
	var err error
	// Set defaults
	ConfigYAML.UDPServiceAddress = defaultUDPServiceAddress
	ConfigYAML.TCPServiceAddress = defaultTCPServiceAddress
	ConfigYAML.MaxUDPPacketSize = maxUDPPacket
	ConfigYAML.BackendType = defaultBackendType
	ConfigYAML.PostFlushCmd = "stdout"
	ConfigYAML.GraphiteAddress = defaultGraphiteAddress
	ConfigYAML.OpenTSDBAddress = defaultOpenTSDBAddress
	ConfigYAML.FlushInterval = flushInterval
	ConfigYAML.LogLevel = "error"
	ConfigYAML.ShowVersion = false
	ConfigYAML.DeleteGauges = true
	ConfigYAML.ResetCounters = true
	ConfigYAML.PersistCountKeys = 0
	ConfigYAML.StatsPrefix = statsPrefixName
	ConfigYAML.StoreDb = dbPath
	ConfigYAML.Prefix = ""
	ConfigYAML.ExtraTags = ""
	ConfigYAML.PercentThreshold = Percentiles{}
	// Percentiles{{Float: 50.0, Str: "50"}, {Float: 80.0, Str: "80"}, {Float: 90.0, Str: "90"}, {Float: 95.0, Str: "95"}}
	ConfigYAML.PrintConfig = false
	ConfigYAML.LogName = "stdout"
	ConfigYAML.LogToSyslog = true
	ConfigYAML.SyslogUDPAddress = "localhost:514"

	Config = ConfigYAML

	os.Setenv("CONFIGOR_ENV_PREFIX", "SD")

	configFile = flag.String("config", "", "Configuration file name (warning not error if not exists). Standard: "+configPath)
	flag.StringVar(&Config.UDPServiceAddress, "udp-addr", ConfigYAML.UDPServiceAddress, "UDP listen service address")
	flag.StringVar(&Config.TCPServiceAddress, "tcp-addr", ConfigYAML.TCPServiceAddress, "TCP listen service address, if set")
	flag.Int64Var(&Config.MaxUDPPacketSize, "max-udp-packet-size", ConfigYAML.MaxUDPPacketSize, "Maximum UDP packet size")
	flag.StringVar(&Config.BackendType, "backend-type", ConfigYAML.BackendType, "MANDATORY: Backend to use: graphite, opentsdb, external, dummy")
	flag.StringVar(&Config.PostFlushCmd, "post-flush-cmd", ConfigYAML.PostFlushCmd, "Command to run on each flush")
	flag.StringVar(&Config.GraphiteAddress, "graphite", ConfigYAML.GraphiteAddress, "Graphite service address")
	flag.StringVar(&Config.OpenTSDBAddress, "opentsdb", ConfigYAML.OpenTSDBAddress, "OpenTSDB service address")
	flag.Int64Var(&Config.FlushInterval, "flush-interval", ConfigYAML.FlushInterval, "Flush interval (seconds)")
	flag.StringVar(&Config.LogLevel, "log-level", ConfigYAML.LogLevel, "Set log level (debug,info,warn,error,fatal)")
	flag.BoolVar(&Config.ShowVersion, "version", ConfigYAML.ShowVersion, "Print version string")
	flag.BoolVar(&Config.DeleteGauges, "delete-gauges", ConfigYAML.DeleteGauges, "Don't send values to graphite for inactive gauges, as opposed to sending the previous value")
	flag.BoolVar(&Config.ResetCounters, "reset-counters", ConfigYAML.ResetCounters, "Reset counters after sending value to backend (send rate) or  send cumulated value (artificial counter - eg. for OpenTSDB & Grafana)")
	flag.Int64Var(&Config.PersistCountKeys, "persist-count-keys", ConfigYAML.PersistCountKeys, "Number of flush-intervals to persist count keys")
	flag.StringVar(&Config.StatsPrefix, "stats-prefix", ConfigYAML.StatsPrefix, "Name for internal application metrics (no prefix prepended)")
	flag.StringVar(&Config.StoreDb, "store-db", ConfigYAML.StoreDb, "Name of database for permanent counters storage (for conversion from rate to counter)")
	flag.StringVar(&Config.Prefix, "prefix", ConfigYAML.Prefix, "Prefix for all stats")
	flag.StringVar(&Config.ExtraTags, "extra-tags", ConfigYAML.ExtraTags, "Default tags added to all measures in format: tag1=value1 tag2=value2")
	flag.Var(&Config.PercentThreshold, "percent-threshold", "Percentile calculation for timers (0-100, may be given multiple times)")
	flag.BoolVar(&Config.PrintConfig, "print-config", ConfigYAML.PrintConfig, "Print config in YAML format")
	flag.StringVar(&Config.LogName, "log-name", ConfigYAML.LogName, "Name of file to log into. If \"stdout\" than logs to stdout.If empty logs go to /dev/null")
	flag.BoolVar(&Config.LogToSyslog, "log-to-syslopg", ConfigYAML.LogToSyslog, "Log to syslog")
	flag.StringVar(&Config.SyslogUDPAddress, "syslog-udp-address", ConfigYAML.SyslogUDPAddress, "Syslog address with port number eg. localhost:514. If empty log to unix socket")
	if parse {
		flag.Parse()
	}

	if len(*configFile) > 0 {
		if _, err = os.Stat(*configFile); os.IsNotExist(err) {
			fmt.Printf("# Warning: No config file: %s\n", *configFile)
			*configFile = ""
		}

		if len(*configFile) > 0 {
			err = configor.Load(&ConfigYAML, *configFile)
			if err != nil {
				fmt.Printf("Error loading config file: %s\n", err)
			} else {
				// set configs read form YAML file

				// save 2 flags
				tmpConfig := Config
				// Overwites flags
				Config = ConfigYAML
				// restore 2 flags
				Config.ShowVersion = tmpConfig.ShowVersion
				Config.PrintConfig = tmpConfig.PrintConfig

			}
		}

		// visitor := func(a *flag.Flag) {
		// 	fmt.Println(">", a.Name, "value=", a.Value)
		// 	switch a.Name {
		// 	case "print-config", "version":
		// 		break
		// 	case "udp-addr":
		// 		ConfigYAML.UDPServiceAddress = a.Value.(string)
		// 	default:
		// 		fmt.Printf("Internal Config Error - unknown variable: %s\n", a.Name)
		// 		os.Exit(1)
		// 	}
		//
		// }
		// flag.Visit(visitor)
	}

	// Normalize prefix
	Config.Prefix = normalizeDot(Config.Prefix, true)

	// Normalize internal metrics name
	Config.StatsPrefix = normalizeDot(Config.StatsPrefix, true)

	// calculate extraFlags hash
	Config.ExtraTagsHash, err = parseExtraTags(Config.ExtraTags)
	if err != nil {
		fmt.Printf("Extra Tags: \"%s\" - %s\n", Config.ExtraTags, err)
		os.Exit(1)
	}

	// Set InternalLogLevel
	Config.InternalLogLevel, err = log.ParseLevel(Config.LogLevel)
	if err != nil {
		fmt.Printf("Invalid log level: \"%s\"\n", Config.LogLevel)
		os.Exit(1)
	}

}
func init() {
	flag.StringVar(&conf, "conf", "config.toml", "Path of the configuration file")
	flag.BoolVar(&version, "version", false, "Return version")
	flag.StringVar(&user, "user", "", "User for MariaDB login, specified in the [user]:[password] format")
	flag.StringVar(&hosts, "hosts", "", "List of MariaDB hosts IP and port (optional), specified in the host:[port] format and separated by commas")
	flag.StringVar(&socket, "socket", "/var/run/mysqld/mysqld.sock", "Path of MariaDB unix socket")
	flag.StringVar(&rpluser, "rpluser", "", "Replication user in the [user]:[password] format")
	flag.BoolVar(&interactive, "interactive", true, "Ask for user interaction when failures are detected")
	flag.BoolVar(&verbose, "verbose", false, "Print detailed execution info")
	flag.StringVar(&preScript, "pre-failover-script", "", "Path of pre-failover script")
	flag.StringVar(&postScript, "post-failover-script", "", "Path of post-failover script")
	flag.Int64Var(&maxDelay, "maxdelay", 0, "Maximum replication delay before initiating failover")
	flag.BoolVar(&gtidCheck, "gtidcheck", false, "Check that GTID sequence numbers are identical before initiating failover")
	flag.StringVar(&prefMaster, "prefmaster", "", "Preferred candidate server for master failover, in host:[port] format")
	flag.StringVar(&ignoreSrv, "ignore-servers", "", "List of servers to ignore in slave promotion operations")
	flag.Int64Var(&waitKill, "wait-kill", 5000, "Wait this many milliseconds before killing threads on demoted master")
	flag.BoolVar(&readonly, "readonly", true, "Set slaves as read-only after switchover")
	flag.StringVar(&failover, "failover", "", "Failover mode, either 'monitor', 'force' or 'check'")
	flag.IntVar(&maxfail, "failcount", 5, "Trigger failover after N failures (interval 1s)")
	flag.StringVar(&switchover, "switchover", "", "Switchover mode, either 'keep' or 'kill' the old master.")
	flag.BoolVar(&autorejoin, "autorejoin", true, "Automatically rejoin a failed server to the current master.")
	flag.StringVar(&logfile, "logfile", "", "Write MRM messages to a log file")
	flag.IntVar(&timeout, "connect-timeout", 5, "Database connection timeout in seconds")
	flag.IntVar(&faillimit, "failover-limit", 0, "In auto-monitor mode, quit after N failovers (0: unlimited)")
	flag.Int64Var(&failtime, "failover-time-limit", 0, "In auto-monitor mode, wait N seconds before attempting next failover (0: do not wait)")
}
예제 #20
0
// Go runs the MailHog sendmail replacement.
func Go() {
	smtpAddr := "localhost:1025"

	goflag := false
	for _, g := range os.Args[1:] {
		if strings.HasPrefix(g, "-") && !strings.HasPrefix(g, "--") {
			if strings.HasPrefix(g, "-from ") || strings.HasPrefix(g, "-from=") ||
				strings.HasPrefix(g, "-smtp-addr ") || strings.HasPrefix(g, "-smtp-addr=") {
				goflag = true
				break
			}
		}
	}

	host, err := os.Hostname()
	if err != nil {
		host = "localhost"
	}

	username := "******"
	user, err := user.Current()
	if err == nil && user != nil && len(user.Username) > 0 {
		username = user.Username
	}

	fromAddr := username + "@" + host
	var recip []string

	if goflag {
		flag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
		flag.StringVar(&fromAddr, "from", fromAddr, "SMTP sender")

		flag.Parse()
		recip = flag.Args()
	} else {
		pflag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
		pflag.StringVarP(&fromAddr, "from", "f", fromAddr, "SMTP sender")

		pflag.Parse()
		recip = pflag.Args()
	}

	body, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error reading stdin")
		os.Exit(11)
	}

	msg, err := mail.ReadMessage(bytes.NewReader(body))
	if err != nil {
		fmt.Fprintln(os.Stderr, "error parsing message body")
		os.Exit(11)
	}

	if len(recip) == 0 {
		// We only need to parse the message to get a recipient if none where
		// provided on the command line.
		recip = append(recip, msg.Header.Get("To"))
	}

	err = smtp.SendMail(smtpAddr, nil, fromAddr, recip, body)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error sending mail")
		log.Fatal(err)
	}

}
예제 #21
0
파일: main.go 프로젝트: heimonsy/minify
func main() {
	output := ""
	mimetype := ""
	filetype := ""
	match := ""
	siteurl := ""

	htmlMinifier := &html.Minifier{}

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [options] [input]\n\nOptions:\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Fprintf(os.Stderr, "\nInput:\n  Files or directories, optional when using piping\n")
	}
	flag.StringVarP(&output, "output", "o", "", "Output (concatenated) file (stdout when empty) or directory")
	flag.StringVar(&mimetype, "mime", "", "Mimetype (text/css, application/javascript, ...), optional for input filenames, has precendence over -type")
	flag.StringVar(&filetype, "type", "", "Filetype (css, html, js, ...), optional for input filenames")
	flag.StringVar(&match, "match", "", "Filename pattern matching using regular expressions, see https://github.com/google/re2/wiki/Syntax")
	flag.BoolVarP(&recursive, "recursive", "r", false, "Recursively minify directories")
	flag.BoolVarP(&hidden, "all", "a", false, "Minify all files, including hidden files and files in hidden directories")
	flag.BoolVarP(&list, "list", "l", false, "List all accepted filetypes")
	flag.BoolVarP(&verbose, "verbose", "v", false, "Verbose")

	flag.StringVar(&siteurl, "url", "", "URL of file to enable URL minification")
	flag.BoolVar(&htmlMinifier.KeepDefaultAttrVals, "html-keep-default-attrvals", false, "Preserve default attribute values")
	flag.BoolVar(&htmlMinifier.KeepWhitespace, "html-keep-whitespace", false, "Preserve whitespace characters but still collapse multiple whitespace into one")
	flag.Parse()
	rawInputs := flag.Args()

	if list {
		var keys []string
		for k := range filetypeMime {
			keys = append(keys, k)
		}
		sort.Strings(keys)
		for _, k := range keys {
			fmt.Println(k + "\t" + filetypeMime[k])
		}
		return
	}

	usePipe := len(rawInputs) == 0
	mimetype = getMimetype(mimetype, filetype, usePipe)

	if match != "" {
		var err error
		pattern, err = regexp.Compile(match)
		if err != nil {
			fmt.Fprintln(os.Stderr, "ERROR: "+err.Error())
			os.Exit(1)
		}
	}

	tasks, dirDst, ok := expandInputs(rawInputs)
	if !ok {
		os.Exit(1)
	}
	if ok = expandOutputs(output, dirDst, usePipe, &tasks); !ok {
		os.Exit(1)
	}

	m = min.New()
	m.AddFunc("text/css", css.Minify)
	m.Add("text/html", htmlMinifier)
	m.AddFunc("text/javascript", js.Minify)
	m.AddFunc("image/svg+xml", svg.Minify)
	m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
	m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)

	var err error
	if m.URL, err = url.Parse(siteurl); err != nil {
		fmt.Fprintln(os.Stderr, "ERROR: "+err.Error())
		os.Exit(1)
	}

	var fails int32
	if verbose {
		for _, t := range tasks {
			if ok := minify(mimetype, t); !ok {
				fails++
			}
		}
	} else {
		var wg sync.WaitGroup
		for _, t := range tasks {
			wg.Add(1)
			go func(t task) {
				defer wg.Done()
				if ok := minify(mimetype, t); !ok {
					atomic.AddInt32(&fails, 1)
				}
			}(t)
		}
		wg.Wait()
	}
	if fails > 0 {
		os.Exit(1)
	}
}
예제 #22
0
func init() {
	pflag.BoolVar(&helpFlag, "help", false, "Show help.")
	pflag.BoolVar(&versionFlag, "version", false, "Shows the current version of Reservoir.")
	pflag.BoolVar(&verboseFlag, "verbose", false, "Be very verbose.")
	pflag.StringVar(&workerDirFlag, "workers", CONFIG_WORKER_DIR, "Use directory for worker configuration files.")
}