func parseFlagsAndArgs() ([]string, error) { // We need to override the default behavior to exit program on parsing error // and to not immediately write errors encountered to Stdout flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) flag.CommandLine.SetOutput(&flagParsingOut) flag.Usage = func() { fmt.Fprintf(&flagParsingOut, "Usage of %s:\n", os.Args[0]) flag.PrintDefaults() } flag.BoolVarP(&verboseFlag, "verbose", "v", false, "Verbose mode") flag.BoolVarP(&helpFlag, "help", "h", false, "help") err := flag.CommandLine.Parse(os.Args[1:]) if verboseFlag { log.Level = logrus.DebugLevel } args := flag.Args() log.Debugf("args are %v", args) if len(args) < 1 { return []string{"", ""}, err } if len(args) < 2 { return []string{args[0], ""}, err } return args, err }
func init() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage: %s\n", usage) flag.PrintDefaults() } }
func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, genSwaggerDocLong+"\n", os.Args[0]) fmt.Fprintf(os.Stderr, genSwaggerDocUsage+"\n", os.Args[0]) fmt.Fprintf(os.Stderr, genSwaggerDocExamples+"\n", os.Args[0]) fmt.Fprintln(os.Stderr, "Options:") flag.PrintDefaults() os.Exit(2) } runtime.GOMAXPROCS(runtime.NumCPU()) flag.Parse() var output io.Writer if outputFile == defaultOutputFile { output = os.Stdout } else { file, err := os.OpenFile(outputFile, os.O_APPEND|os.O_WRONLY, os.ModeAppend) if err != nil { fmt.Fprintf(os.Stderr, "Error reading output file: %v\n", err) } defer file.Close() output = file } documentationForTypes := kruntime.ParseDocumentationFrom(inputFile) if verify { if numMissingDocs, err := kruntime.VerifySwaggerDocsExist(documentationForTypes, output); err != nil { fmt.Fprintf(os.Stderr, "Error verifying documentation: %v\n", err) os.Exit(1) } else if numMissingDocs != 0 { fmt.Fprintf(os.Stderr, "Found %d types or fields missing documentation in %q.\n", numMissingDocs, inputFile) os.Exit(1) } fmt.Fprintf(os.Stdout, "Found no types or fields missing documentation in %q.\n", inputFile) os.Exit(0) } if err := kruntime.WriteSwaggerDocFunc(documentationForTypes, output); err != nil { fmt.Fprintf(os.Stderr, "Error writing generated Swagger documentation to file: %v\n", err) os.Exit(1) } }
// syntaxError prints the syntax func syntaxError() { fmt.Fprintf(os.Stderr, `Sync files and directories to and from local and remote object stores - %s. Syntax: [options] subcommand <parameters> <parameters...> Subcommands: `, fs.Version) for i := range Commands { cmd := &Commands[i] fmt.Fprintf(os.Stderr, " %s %s\n", cmd.Name, cmd.ArgsHelp) fmt.Fprintf(os.Stderr, "%s\n\n", cmd.Help) } fmt.Fprintf(os.Stderr, "Options:\n") pflag.PrintDefaults() fmt.Fprintf(os.Stderr, ` It is only necessary to use a unique prefix of the subcommand, eg 'mo' for 'move'. `) }
func usage() { fmt.Fprintf(os.Stderr, "Usage: %s [options] -- command\n", os.Args[0]) pflag.PrintDefaults() }
// Usage is what is run if the right parameters are not met upon startup. func Usage() { // To embed the bot user and password comment the line above and uncomment the line below fmt.Printf("Usage: %v -i <ip address> -p <port> -d <domain name>\n", runAs) flag.PrintDefaults() }
func main() { // Command line options var etcdAddress, domain, confPath, logLevel string flag.StringVarP(&domain, "domain", "d", "", "domain for lochness; required") flag.StringVarP(&etcdAddress, "etcd", "e", "http://127.0.0.1:4001", "address of etcd server") flag.StringVarP(&confPath, "conf-dir", "c", "/etc/dhcp/", "dhcpd configuration directory") flag.StringVarP(&logLevel, "log-level", "l", "warning", "log level: debug/info/warning/error/critical/fatal") flag.Parse() // Domain is required if domain == "" { flag.PrintDefaults() os.Exit(1) } // Logging if err := logx.DefaultSetup(logLevel); err != nil { log.WithFields(log.Fields{ "error": err, "func": "logx.DefaultSetup", }).Fatal("Could not set up logrus") } hconfPath := path.Join(confPath, "hypervisors.conf") gconfPath := path.Join(confPath, "guests.conf") // Set up fetcher and refresher f := NewFetcher(etcdAddress) r := NewRefresher(domain) err := f.FetchAll() if err != nil { os.Exit(1) } // Update at the start of each run restart, err := updateConfigs(f, r, hconfPath, gconfPath) if restart { restartDhcpd() } if err != nil { os.Exit(1) } // Create the watcher w, err := watcher.New(f.etcdClient) if err != nil { log.WithFields(log.Fields{ "error": err, "func": "watcher.New", }).Fatal("Could not create watcher") } // Start watching the necessary etcd prefixs prefixes := [...]string{"/lochness/hypervisors", "/lochness/guests", "/lochness/subnets"} for _, prefix := range prefixes { if err := w.Add(prefix); err != nil { log.WithFields(log.Fields{ "error": err, "func": "watcher.Add", "prefix": prefix, }).Fatal("Could not add watch prefix") } } // Channel for indicating work in progress // (to coordinate clean exiting between the consumer and the signal handler) ready := make(chan struct{}, 1) ready <- struct{}{} for w.Next() { // Remove item to indicate processing has begun done := <-ready // Integrate the response and update the configs if necessary refresh, err := f.IntegrateResponse(w.Response()) if err != nil { log.Info("Error on integration; re-fetching") err := f.FetchAll() if err != nil { os.Exit(1) } refresh = true } if refresh { restart, err := updateConfigs(f, r, hconfPath, gconfPath) if restart { restartDhcpd() } if err != nil { log.WithFields(log.Fields{ "error": err, "func": "updateConfigs", }).Warn("Could not create watcher") } } // Return item to indicate processing has completed ready <- done } if err := w.Err(); err != nil { log.WithField("error", err).Fatal("Watcher encountered an error") } // Handle signals for clean shutdown sigs := make(chan os.Signal) signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) s := <-sigs log.WithField("signal", s).Info("Signal received; waiting for current task to process") <-ready // wait until any current processing is finished _ = w.Close() log.Info("Exiting") }
// PrintDefaults prints default value for all elements func PrintDefaults() { pflag.PrintDefaults() }