Example #1
0
func init() {
	flag.BoolVarP(&Version, "version", "v", false,
		"Output the current version of the application.")

	flag.StringVarP(&WorkingDir, "working-dir", "w", "./",
		"The directory where all the other directories reside. This "+
			"will be prepended to the rest of the configurable directories.")

	flag.StringVarP(&OutputDir, "output-dir", "o", "public",
		"The directory where the results should be placed.")

	flag.BoolVarP(&EmptyOutputDir, "empty-output-dir", "x", false,
		"Before writing to the output-dir, delete anything inside of it.")

	flag.StringVarP(&TemplateDir, "template-dir", "t", "templates",
		"The directory where the site templates are located.")

	flag.StringVarP(&BlogDir, "blog-dir", "b", "blogs",
		"The directory where the blogs are located.")

	flag.StringVarP(&StaticDir, "static-dir", "s", "static",
		"The directory where the static assets are located.")

	flag.StringVarP(&URL, "url", "u", "",
		"The url to be prepended to link in the RSS feed. Defaults to "+
			"the value in the channel <link>.")

	flag.IntVarP(&MaxIndexEntries, "index-entries", "i", 3,
		"The maximum number of entries to display on the index page.")

}
Example #2
0
// Start the game and then, based on command line input, start up client
// interface listeners. Finally, wait for ^C before exiting.
func main() {
	var local, serve bool
	var endpoint string
	pflag.BoolVarP(&local, "local", "l", true, "run a local interactive REPL")
	pflag.BoolVarP(&serve, "serve", "s", false, "run a networked multiuser server")
	pflag.StringVarP(&endpoint, "endpoint", "e", ":4000", "where to run the networked server")
	pflag.Parse()

	done, events, spellbook := StartGame()

	if serve {
		go ListenTCP(endpoint, events, spellbook)
	}
	if local {
		go ListenLocal(events, spellbook)
	}

	interrupt := make(chan os.Signal)
	signal.Notify(interrupt, os.Interrupt)

	quitting := false
	for !quitting {
		select {
		case <-interrupt:
			events <- &ShutdownEvent{}
		case err := <-done:
			log.Print("main() stopping: ", err)
			quitting = true
		}
	}
}
Example #3
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)
}
Example #4
0
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)")
}
Example #5
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)
}
Example #6
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)
	}
}
Example #7
0
File: main.go Project: 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
}
Example #8
0
func init() {
	flag.BoolVarP(&flagVerbose, "verbose", "v", false, "be verbose")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: binscope [options] <file1>...\n")
		flag.PrintDefaults()
	}
}
Example #9
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
}
Example #10
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()
}
Example #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")
		}
	}
}
Example #12
0
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")
}
Example #13
0
File: main.go Project: 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
}
Example #14
0
// Initial setup when the program starts.
func setup() {
	// ensure that zpool/zfs commands do not use localized messages:
	os.Setenv("LC_ALL", "C")

	// command line flags:
	pflag.StringVarP(&cfgFile, "conf", "c", CFGFILE, "configuration file path")
	pflag.BoolVarP(&optDebug, "debug", "d", false, "print debug information to stdout")
	optHashPassword := pflag.BoolP("passwordhash", "P", false, "hash web password")
	optTest := pflag.BoolP("test", "t", false, "test configuration and exit")
	optVersion := pflag.BoolP("version", "v", false, "print version information and exit")

	pflag.Parse()

	if pflag.NArg() > 0 {
		pflag.Usage()
		os.Exit(2)
	}
	if *optVersion {
		version()
		os.Exit(0)
	}
	if *optHashPassword {
		wwwHashPassword()
		os.Exit(0)
	}

	// initialize logging & notification:

	if *optTest {
		optDebug = true
	}

	cfg = getCfg()
	if cfg == nil {
		os.Exit(2)
	}
	notify = setupLog(cfg)

	if *optTest {
		notifyCloseC := notify.Close()
		select { // wait max 1 second for loggers to finish
		case <-notifyCloseC:
		case <-time.After(time.Second):
		}
		os.Exit(0)
	}
}
Example #15
0
func main() {
	var err error
	var cp string
	var initial bool
	var conferr error

	flag.StringVarP(&cp, "conf", "c", "conf.yml", "Local path to configuration file.")
	flag.BoolVarP(&initial, "initial", "i", false, "Run the initial setup of the server.")
	flag.Parse()

	conferr = conf.Load(cp)
	if conferr != nil || initial {
		setup.Run()
	}
	if err = utils.EnsureDir(conf.C.UploadDir); err != nil {
		log.Fatal(err)
	}
	if db, err = gorm.Open("sqlite3", conf.C.DB); err != nil {
		log.Fatal(err)
	}
	db.AutoMigrate(&models.ResourceEntry{})

	go monitoring.Monit(&db)

	log.Printf("[INFO][System]\tStarted goploader server on port %d\n", conf.C.Port)
	if !conf.C.Debug {
		gin.SetMode(gin.ReleaseMode)
	}
	// Default router
	r := gin.Default()
	// Templates and static files
	r.LoadHTMLGlob("templates/*")
	r.Static("/static", "./assets")
	r.Static("/favicon.ico", "./assets/favicon.ico")
	// Routes
	r.GET("/", index)
	r.POST("/", create)
	r.GET("/v/:uniuri/:key", view)
	// Run
	r.Run(fmt.Sprintf(":%d", conf.C.Port))
}
Example #16
0
func main() {
	var err error
	var cp string
	var initial bool
	var r *gin.Engine

	tbox, _ := rice.FindBox("templates")
	abox, _ := rice.FindBox("assets")

	flag.StringVarP(&cp, "conf", "c", "conf.yml", "Local path to configuration file.")
	flag.BoolVarP(&initial, "initial", "i", false, "Run the initial setup of the server.")
	flag.Parse()

	if err = conf.Load(cp, !initial); err != nil || initial {
		setup.Run()
	}
	if err = database.Initialize(); err != nil {
		log.Fatal(err)
	}
	defer database.DB.Close()
	if err = models.Initialize(); err != nil {
		log.Fatal(err)
	}
	go monitoring.Monit()
	if r, err = router.Setup(tbox, abox); err != nil {
		log.Fatal(err)
	}

	logger.Info("server", "Started goploader server on port", conf.C.Port)
	if conf.C.ServeHTTPS {
		if err = http.ListenAndServeTLS(fmt.Sprintf(":%d", conf.C.Port), conf.C.SSLCert, conf.C.SSLPrivKey, r); err != nil {
			logger.Err("server", "Fatal error", err)
		}
	} else {
		if err = r.Run(fmt.Sprintf(":%d", conf.C.Port)); err != nil {
			logger.Err("server", "Fatal error", err)
		}
	}
}
Example #17
0
func init() {
	flag.StringVarP(&configpath, "config", "c", "/etc/stout/stout-default.conf", "path to a configuration file")
	flag.BoolVarP(&showVersion, "version", "v", false, "show version and exit")
	flag.Parse()
}
Example #18
0
func main() {
	d := deferer.NewDeferer(nil)
	defer d.Run()

	rand.Seed(time.Now().UnixNano())
	id := rand.Int()
	if ID := os.Getenv("ID"); ID != "" {
		fmt.Sscanf(ID, "%d", &id)
	}

	params := params{ID: id}
	flag.Uint64VarP(&params.Interval, "interval", "i", 30, "Interval in seconds to refresh lock")
	flag.Uint64VarP(&params.TTL, "ttl", "t", 0, "TTL for key in seconds, leave 0 for (2 * interval)")
	flag.StringVarP(&params.Key, "key", "k", "/lock", "Key to use as lock")
	flag.BoolVarP(&params.Blocking, "block", "b", false, "Block if we failed to acquire the lock")
	flag.StringVarP(&params.Addr, "etcd", "e", defaultAddr, "address of etcd machine")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s: [options] -- command args\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Fprintf(os.Stderr, "\ncommand will be run with args via fork/exec not a shell\n")
	}
	flag.Parse()

	if params.TTL == 0 {
		params.TTL = params.Interval * 2
	}

	params.Args = flag.Args()
	if len(params.Args) < 1 {
		d.Fatal("command is required")
	}
	cmd := resolveCommand(params.Args[0])
	if cmd == "" {
		d.Fatal("")
	}
	params.Args[0] = cmd

	hostname, err := os.Hostname()
	if err != nil {
		d.FatalWithFields(log.Fields{
			"error": err,
			"func":  "os.Hostname",
		}, "failed to get hostname")
	}

	c := etcd.NewClient([]string{params.Addr})
	l, err := lock.Acquire(c, params.Key, hostname, params.TTL, params.Blocking)
	if err != nil {
		d.FatalWithFields(log.Fields{
			"error":    err,
			"func":     "lock.Acquire",
			"lock":     params.Key,
			"ttl":      params.TTL,
			"blocking": params.Blocking,
		}, "failed to get lock")
	}

	d.Defer(func() {
		if err := l.Release(); err != nil {
			d.FatalWithFields(log.Fields{
				"error": err,
				"func":  "l.Release",
			}, "failed to release lock")
		}
	})
	params.Lock = l

	args, err := json.Marshal(&params)
	if err != nil {
		d.FatalWithFields(log.Fields{
			"error": err,
			"func":  "json.Marshal",
		}, "failed to serialize params")
	}

	serviceDone := make(chan struct{})
	base := filepath.Base(params.Args[0])
	target := fmt.Sprintf("locker-%s-%d.service", base, id)
	locker := resolveCommand("locker")
	if locker == "" {
		d.Fatal("")
	}
	go runService(d, serviceDone, params.ID, params.TTL, target, locker, base, string(args))

	sigs := make(chan os.Signal)
	signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)

	select {
	case <-serviceDone:
		log.WithField("service_state", "done").Info("service is done")
	case s := <-sigs:
		log.WithField("signal", s).Info("signal received")
		if err := killService(target, int32(s.(syscall.Signal))); err != nil {
			log.WithField("error", err).Info("failed to kill service")
		}
	}
}
Example #19
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)
	}
}
Example #20
0
func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "nrcq(8)             System Administration Utilities            nrcq(8)\n")
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "NAME\n")
		fmt.Fprintf(os.Stderr, "  nrcq - NagRestConf Query utility\n")
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "SYNOPSIS\n")
		fmt.Fprintf(os.Stderr, "  nrcq [options] URL ENDPOINT\n")
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "DESCRIPTION\n")
		flag.PrintDefaults()
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "EXAMPLES\n")
		fmt.Fprintf(os.Stderr, "  Show all valid endpoints:\n")
		fmt.Fprintf(os.Stderr, "    nrcq -L\n")
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "  List all nagios options for the servicesets table:\n")
		fmt.Fprintf(os.Stderr, "    nrcq -l servicesets\n")
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "  Show all hosts:\n")
		fmt.Fprintf(os.Stderr, "    nrcq http://server/rest show/hosts\n")
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "  Show a subset of hosts using a simple RE2 regular expression:\n")
		fmt.Fprintf(os.Stderr, "    nrcq http://server/rest show/hosts")
		fmt.Fprintf(os.Stderr, " -f \"name:host2\"\n")
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "  Show a subset of services using a complex RE2 regular expression:\n")
		fmt.Fprintf(os.Stderr, "  (See https://github.com/google/re2/wiki/Syntax)\n")
		fmt.Fprintf(os.Stderr, "    nrcq http://server/rest show/services")
		fmt.Fprintf(os.Stderr, " -f \"name:\\bhost2\\b|web,svcdesc:(?i)swap\"\n")
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "  Add a new host:\n")
		fmt.Fprintf(os.Stderr, "    nrcq http://server/rest add/hosts \\\n")
		fmt.Fprintf(os.Stderr, "      -d name:server1 \\\n")
		fmt.Fprintf(os.Stderr, "      -d alias:server1 \\\n")
		fmt.Fprintf(os.Stderr, "      -d ipaddress:server1.there.gq \\\n")
		fmt.Fprintf(os.Stderr, "      -d template:hsttmpl-local \\\n")
		fmt.Fprintf(os.Stderr, "      -d servicesets:example-lin\n")
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "  Delete a host and all of its services:\n")
		fmt.Fprintf(os.Stderr, "    nrcq http://server/rest delete/services \\\n")
		fmt.Fprintf(os.Stderr, "      -d name:server1 \\\n")
		fmt.Fprintf(os.Stderr, "      -d \"svcdesc:.*\"\n")
		fmt.Fprintf(os.Stderr, "    nrcq http://server/rest delete/hosts \\\n")
		fmt.Fprintf(os.Stderr, "      -d name:server1 \\\n")
		fmt.Fprintf(os.Stderr, "\n")
	}

	flag.StringVarP(&Args.folder, "folder", "F", "local",
		"The system folder to query.")
	flag.StringVarP(&Args.filter, "filter", "f", "",
		"A client side RE2 regex filter, 'option:regex[,option:regex]...'")
	flag.BoolVarP(&Args.version, "version", "v", false,
		"Show the version of this program.")
	flag.BoolVarP(&Args.newline, "pack", "p", false,
		"Remove spaces and lines from the Json output.")
	flag.BoolVarP(&Args.brief, "complete", "c", false,
		"Also show fields with empty values.")
	// Done automatically now:
	//flag.BoolVarP(&Args.encode, "encode", "e", false,
	//	"URL Encode output where necessary so it can be piped to another tool.")
	flag.BoolVarP(&Args.listendpoints, "listendpoints", "L", false,
		"List all endpoints/tables.")
	flag.StringVarP(&Args.username, "username", "U", "",
		"Username for Basic Auth.")
	flag.StringVarP(&Args.password, "password", "P", "",
		"Password for Basic Auth.")
	flag.StringVarP(&Args.list, "list", "l", "",
		"List all options for the specified table. Required fields are\n\t preceded by a star, '*'.")
	flag.BoolVarP(&Args.json, "json", "j", false,
		"Output in JSON format.")
	flag.VarP(&dataFlag, "data", "d",
		"Set extra data to send, 'option:value'.\n\tThe user should not urlencode data, nrcq will do it.\n\tMay be used multiple times.")
}
Example #21
0
func main() {
	var err error
	var datasource io.Reader
	var file string

	var tee bool
	var progress bool
	var clip bool
	var argname string
	var screen bool
	var delay time.Duration
	var window bool
	var lifetime string
	var once bool
	var hostname string

	flag.BoolVarP(&tee, "tee", "t", false, "Displays stdin to stdout")
	flag.BoolVarP(&progress, "progress", "p", false, "Displays a progress bar")
	flag.BoolVarP(&clip, "clipboard", "c", false, "Copy the returned URL directly to the clipboard (needs xclip or xsel)")
	flag.BoolVarP(&verbose, "verbose", "v", false, "Activates the debug mode")
	flag.StringVarP(&argname, "name", "n", "", "Specify the filename you want")
	flag.StringVarP(&lifetime, "lifetime", "l", "1d", "Specify the lifetime of your file (30m, 1h, 6h, 1d, 1w)")
	flag.BoolVarP(&screen, "screenshot", "s", false, "Screenshot and uploads your current screen (Need the `import` command)")
	flag.DurationVarP(&delay, "delay", "d", 0, "Define a delay before the program executes (including taking the screenshot)")
	flag.BoolVarP(&window, "window", "w", false, "Click on the window you want to screenshot (only works with -s/--screenshot option)")
	flag.BoolVarP(&once, "once", "o", false, "Your upload will be visible only once and then deleted from the server")
	flag.StringVarP(&hostname, "hostname", "", "", "Specify the server you want to send your file to")

	flag.Parse()
	args := flag.Args()
	debugf("Debug mode is activated")

	if err = conf.Load(); err != nil {
		log.Fatal(err)
	}

	if delay != 0 {
		debugf("Waiting", delay)
		time.Sleep(delay)
	}
	if len(args) > 0 {
		file = args[0]
	}
	if screen {
		debugf("Executing screenshot")
		file = "/tmp/goploader-screen.png"
		if err = screenshot.Do(file, window); err != nil {
			log.Fatal(err)
		}
		defer func() {
			debugf("Removing temporary screenshot", file)
			err = os.Remove(file)
			if err != nil {
				log.Fatal(err)
			}
		}()
	}

	if file != "" {
		debugf("Main datasource is", file)
		var f *os.File
		f, err = os.Open(file)
		check(err)
		defer f.Close()
		name = filepath.Base(f.Name())
		datasource = f
		if progress {
			debugf("Initialization of known progress bar")
			initBar(f)
		}
	} else {
		debugf("Main datasource is stdin")
		name = "stdin"
		datasource = os.Stdin
		if progress {
			debugf("Initalization of unknown progress bar")
			initUnknownBar()
		}
	}
	if tee {
		debugf("Main datasource is now a TeeReader")
		datasource = io.TeeReader(datasource, os.Stdout)
	}
	if argname != "" {
		debugf("Name was given as argument")
		name = argname
	}

	debugf("Initialization pipe")
	r, w := io.Pipe()
	multipartWriter := multipart.NewWriter(w)
	go func() {
		debugf("Started the goroutine to pipe data")
		var part io.Writer
		defer w.Close()
		defer multipartWriter.Close()
		multipartWriter.WriteField("duration", lifetime)
		if once {
			multipartWriter.WriteField("once", "true")
		}
		if part, err = multipartWriter.CreateFormFile("file", name); err != nil {
			log.Fatal(err)
		}
		if progress {
			part = io.MultiWriter(part, bar)
		}
		if _, err = io.Copy(part, datasource); err != nil {
			log.Fatal(err)
		}
	}()

	debugf("Executing multipart post")
	if hostname == "" {
		hostname = conf.C.Service
	}
	resp, err := http.Post(hostname, multipartWriter.FormDataContentType(), r)
	check(err)
	defer resp.Body.Close()
	debugf("Multipart post is done, reading data")
	ret, err := ioutil.ReadAll(resp.Body)
	check(err)
	if clip {
		debugf("Copying to clipboard")
		clipboard.WriteAll(string(ret))
		fmt.Print("Copied URL to clipboard\n")
	} else {
		fmt.Print(string(ret))
	}
}
Example #22
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)
	}
}