Пример #1
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()
}
Пример #2
0
func init() {
	flag.Usage = printUsage

	// set the defaults
	defaultSSL = true
	defaultPort = 38332
	defaultAddress = "localhost"

	defaultConfigPath := computeDefaultConfigPath()

	flag.StringVarP(&flagConfigPath, "config", "c", defaultConfigPath, "config file path")
	flag.StringVarP(&flagUsername, "rpcuser", "u", "", "rpc user name")
	flag.StringVarP(&flagPassword, "rpcpassword", "p", "", "rpc password")
	flag.BoolVar(&flagSSL, "ssl", defaultSSL, "use secure sockets SSL")
	flag.IntVarP(&flagPort, "port", "n", defaultPort, "port number")
	flag.StringVarP(&flagAddress, "host", "a", defaultAddress, "server address")

	flag.BoolVarP(&verbose, "verbose", "v", false, "verbose output for debugging")
	// k , ignore server certificate errors (not recommended, only for testing self-signed certificates")

	flag.BoolVar(&flagHelp, "help", false, "Display this information")
	flag.BoolVar(&flagVersion, "version", false, "Display version information")
	flag.BoolVarP(&flagInsecure, "insecure", "k", false, "Allow connections to servers with 'insecure' SSL connections e.g. self-signed certificates. By default, this option is false, unless the server is localhost or an explicit IP address, in which case the option is true.")

	flag.Parse()

	flagSetMap = make(map[string]bool)
	flag.Visit(visitFlagSetMap)
}
Пример #3
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)
	}
}
Пример #4
0
func init() {
	flag.BoolVar(&flagListProtocols, "list-protocols", false,
		"list all registered protocols and then exit")
	flag.BoolVar(&flagUseTransparent, "transparent", false,
		"use transparent proxying (only available on Linux)")
	flag.Uint16VarP(&flagListenPort, "port", "p", 0,
		"port to listen on")
	flag.StringVarP(&flagListenHost, "host", "h", "0.0.0.0",
		"host to listen on")
}
Пример #5
0
// Main entry point
func main() {
	// Set defaults for the server configuration with BaseConfiguration
	// inside of MyConfiguration.
	Conf = &MyConfiguration{
		BaseConfiguration: goserv.BaseConfiguration{
			BindAddress:    "0.0.0.0",
			BindPort:       80,
			ReadTimeout:    10 * time.Second,
			WriteTimeout:   10 * time.Second,
			MaxHeaderBytes: 1 << 20,
			LogLevel:       "debug",
		},
		Test: false,
	}

	// Add a flag for our custom Test addition to the Configuration
	flag.BoolVar(&Conf.Test, "Test", Conf.Test, "Example of extending the Configuration")

	// Simple handler to say hello!
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello!"))
	})

	// Make a new server.
	server := goserv.NewServer(&Conf.BaseConfiguration)
	// Log that we are listing
	goserv.Logger.Infof("Listening for connections on %s", server.Addr)
	// And listen for connections
	server.ListenAndServe()
}
Пример #6
0
func parseArgs() args {
	result := args{}
	pflag.Usage = usage
	pflag.BoolVarP(&result.options.KeepGoing, "keep-going", "k", false, "")
	pflag.BoolVar(&result.options.CheckAll, "check-all", false, "")
	pflag.StringVarP(&result.scriptFile, "file", "f", "", "")
	verbose := pflag.BoolP("verbose", "v", false, "")
	quiet := pflag.BoolP("quiet", "q", false, "")
	topics := pflag.String("debug", "", "")
	pflag.Parse()
	if *topics != "" {
		result.debugTopics = strings.Split(*topics, ",")
	}

	// argh: really, we just want a callback for each occurence of -q
	// or -v, which decrements or increments verbosity
	if *quiet {
		result.verbosity = 0
	} else if *verbose {
		result.verbosity = 2
	} else {
		result.verbosity = 1
	}

	result.options.Targets = pflag.Args()
	return result
}
Пример #7
0
func main() {
	var profile, version bool
	flag.BoolVarP(&version, "version", "v", false, "\tShow version")
	flag.BoolVar(&profile, "profile", false, "\tEnable profiler")
	flag.Parse()

	InitConfig()

	if profile {
		f, err := os.Create("alexandria.prof")
		if err != nil {
			panic(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	// TODO run when there is something new render.GenerateIndex()

	if version {
		fmt.Println(NAME, VERSION)
		return
	}

	err := qml.Run(run)
	if err != nil {
		panic(err)
	}
}
Пример #8
0
func main() {
	var profile, version bool
	flag.BoolVarP(&version, "version", "v", false, "\tShow version")
	flag.BoolVar(&profile, "profile", false, "\tEnable profiler")
	flag.Parse()

	InitConfig()
	Config.MaxResults = 20

	if profile {
		f, err := os.Create("alexandria.prof")
		if err != nil {
			panic(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	// TODO run GenerateIndex() when there is something new

	if version {
		fmt.Println(NAME, VERSION)
		return
	}

	http.HandleFunc("/", mainHandler)
	http.HandleFunc("/stats", statsHandler)
	http.HandleFunc("/search", queryHandler)
	http.HandleFunc("/alexandria.edit", editHandler)
	serveDirectory("/images/", Config.CacheDirectory)
	serveDirectory("/static/", Config.TemplateDirectory+"static")
	http.ListenAndServe("127.0.0.1:41665", nil)
}
Пример #9
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")

}
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)")
}
Пример #11
0
func main() {
	var index, profile, stats, version bool
	flag.BoolVarP(&index, "index", "i", false, "\tUpdate the index")
	flag.BoolVarP(&stats, "stats", "S", false, "\tPrint some statistics")
	flag.BoolVarP(&version, "version", "v", false, "\tShow version")
	flag.BoolVar(&profile, "profile", false, "\tEnable profiler")
	flag.Parse()

	InitConfig()
	Config.MaxResults = 1e9

	if profile {
		f, err := os.Create("alexandria.prof")
		if err != nil {
			panic(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	switch {
	case index:
		GenerateIndex()
	case stats:
		printStats()
	case version:
		fmt.Println(NAME, VERSION)
	default:
		i := 1
		if len(os.Args) > 0 {
			if os.Args[1] == "--" {
				i += 1
			} else if os.Args[1] == "all" {
				fmt.Printf("Rendered all %d scrolls.\n", RenderAllScrolls())
				os.Exit(0)
			}
		}
		results, err := FindScrolls(strings.Join(os.Args[i:], " "))
		if err != nil {
			panic(err)
		}
		fmt.Printf("There are %d matching scrolls.\n", len(results.Ids))
		for _, id := range results.Ids {
			fmt.Println("file://" + Config.CacheDirectory + string(id.Id) + ".png")
		}
	}
}
Пример #12
0
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
}
Пример #13
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()
}
Пример #14
0
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)
	}
}
Пример #15
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)
	}

}
Пример #16
0
Файл: main.go Проект: ppg/rosgo
func main() {
	log.SetFlags(0)

	flag.StringVarP(&infile, "in", "i", "", "input file")
	flag.StringVarP(&outfile, "out", "o", "", "output file; defaults to '<input file>.go'")
	flag.StringVarP(&packageName, "package", "p", "", "package name for generated file; defaults to 'msgs' or 'srvs'")
	flag.BoolVar(&dryRun, "dry-run", false, "output the file that would be generated to stdout")
	flag.Parse()

	if flag.NArg() < 1 {
		log.Printf("must provide type of generator")
		flag.PrintDefaults()
		os.Exit(1)
	}
	templateType := flag.Arg(0)
	if packageName == "" {
		packageName = templateType + "s"
	}

	basename := fmt.Sprintf("%s.tmpl", templateType)
	data, err := Asset(basename)
	if err != nil {
		log.Printf("unrecognized generator template: %s (%s)", templateType, err)
		flag.PrintDefaults()
		os.Exit(1)
	}
	tmpl := template.New(basename)
	tmpl = tmpl.Funcs(map[string]interface{}{
		// HACK(ppg): Allow setting a loop variable a struct so we can use it
		"setloopvar": func(setter loopVarSetter, value interface{}) interface{} {
			setter.SetLoopVar(value)
			return setter
		},
	})
	tmpl, err = tmpl.Parse(string(data))
	if err != nil {
		log.Printf("unable to template %s: %s", templateType, err)
		os.Exit(1)
	}

	data, err = Asset("msg.partial.tmpl")
	if err != nil {
		log.Printf("unrecognized generator template: %s (%s)", templateType, err)
		flag.PrintDefaults()
		os.Exit(1)
	}
	tmpl2 := tmpl.New("msg.partial.tmpl")
	_, err = tmpl2.Parse(string(data))
	if err != nil {
		log.Printf("unable to template %s: %s", templateType, err)
		os.Exit(1)
	}

	if flag.NArg() > 1 {
		log.Printf("unrecognized arguments: %v", flag.Args()[1:])
		flag.PrintDefaults()
		os.Exit(1)
	}

	if infile == "" {
		log.Printf("must provide input file")
		flag.PrintDefaults()
		os.Exit(1)
	}
	if outfile == "" {
		outfile = infile + ".go"
	}

	// Read input file
	data, err = ioutil.ReadFile(infile)
	if err != nil {
		log.Fatalf("failed to read infile %s: %s", infile, err)
	}
	basename = filepath.Base(infile)
	fileInfo := FileInfo{
		InFile:      infile,
		InFileBase:  filepath.Base(infile),
		Raw:         string(data),
		MD5Sum:      fmt.Sprintf("%x", md5.Sum(data)),
		PackageName: packageName,
		Name:        strings.TrimSuffix(basename, filepath.Ext(basename)),
	}

	// Parse by type
	var spec interface{}
	switch templateType {
	case "msg":
		var msgSpec *MsgSpec
		msgSpec, err = parseMsgSpec(fileInfo.PackageName, fileInfo.Name, data)
		if err != nil {
			log.Fatalf("failed to parse %s spec: %s", templateType, err)
		}
		spec = msgSpec

	case "srv":
		var srvSpec *SrvSpec
		srvSpec, err = parseSrvSpec(fileInfo.PackageName, fileInfo.Name, data)
		if err != nil {
			log.Fatalf("failed to parse %s spec: %s", templateType, err)
		}
		spec = srvSpec

	default:
		log.Fatalf("no parser configured: %s", templateType)
	}

	buf := bytes.NewBuffer([]byte{})
	err = tmpl.Execute(buf, map[string]interface{}{"FileInfo": fileInfo, "Spec": spec})
	if err != nil {
		log.Fatalf("failed to generate Go file: %s", err)
	}

	fset := token.NewFileSet()
	ast, err := parser.ParseFile(fset, "", buf.Bytes(), parser.ParseComments)
	if err != nil {
		log.Fatalf("bad Go source code was generated: %s\n%s", err, buf.String())
	}
	buf.Reset()
	err = (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(buf, fset, ast)
	if err != nil {
		log.Fatalf("generated Go source code could not be reformatted: %s", err)
	}

	if dryRun {
		fmt.Println(buf.String())
		return
	}

	err = ioutil.WriteFile(outfile, buf.Bytes(), 0644)
	if err != nil {
		log.Fatalf("failed to write go file: %s", err)
	}
	log.Printf("Wrote %s from %s", outfile, infile)
}
Пример #17
0
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)
	}
}
Пример #18
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.")
}
Пример #19
0
func init() {
	flag.BoolVar(&generateTestFiles, "generate", false, "Indicates that test files should be generated.")
}