Beispiel #1
0
// Parse the command from the command line
func ParseCommand() (*Command, []string) {
	args := pflag.Args()
	if len(args) < 1 {
		fatal("No command supplied\n")
	}

	cmd := strings.ToLower(args[0])
	args = args[1:]

	// Find the command doing a prefix match
	var command *Command
	for i := range Commands {
		trialCommand := &Commands[i]
		// exact command name found - use that
		if trialCommand.Name == cmd {
			command = trialCommand
			break
		} else if strings.HasPrefix(trialCommand.Name, cmd) {
			if command != nil {
				fs.Stats.Error()
				log.Fatalf("Not unique - matches multiple commands %q", cmd)
			}
			command = trialCommand
		}
	}
	if command == nil {
		fs.Stats.Error()
		log.Fatalf("Unknown command %q", cmd)
	}
	if command.Run == nil {
		syntaxError()
	}
	command.checkArgs(args)
	return command, args
}
Beispiel #2
0
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
}
Beispiel #3
0
func main() {
	pflag.Parse()

	p2p.PrintMessages(printNetworkMessages)

	log.SetPrefix(p2pConfig.Name + " ")

	network := p2p.New(p2pConfig)
	network.Join(pflag.Args())

	cfg := libsyncer.Config{
		FileServerConfig: fsConfig,
		PriceFormula:     pricer(),
		Transport:        network,
		Volume:           volume(),
	}
	syncer := libsyncer.New(cfg)
	go syncer.Serve()

	ch := make(chan os.Signal)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
	<-ch

	log.Println("Received shutdown signal. Stopping ...")
	syncer.Stop()

	if err := network.Leave(10 * time.Second); err != nil {
		log.Printf("ERROR: %v", err)
	}
}
Beispiel #4
0
func parseArgs() {
	pflag.Usage = usage
	pflag.Parse()
	if pflag.NArg() < 1 {
		log.Fatal("must specify a command")
	}
	command = pflag.Args()
	timeoutMS = uint64(timeoutDur / time.Millisecond)
}
Beispiel #5
0
func parseFlagsAndArgs() []string {
	flag.BoolVarP(&verboseFlag, "verbose", "v", false, "Verbose mode")
	flag.BoolVarP(&helpFlag, "help", "h", false, "help")
	flag.Parse()
	if verboseFlag {
		log.Level = logrus.DebugLevel
	}
	if err := flag.CommandLine.Parse(os.Args[1:]); err != nil {
		flag.Usage()
		os.Exit(1)
	}
	args := flag.Args()
	if len(args) < 2 {
		return []string{args[0], ""}
	}
	return args
}
Beispiel #6
0
// ParseCommand parses the command from the command line
func ParseCommand() (*Command, []string) {
	args := pflag.Args()
	if len(args) < 1 {
		fatal("No command supplied\n")
	}

	cmd := strings.ToLower(args[0])
	args = args[1:]

	// Find the command doing a prefix match
	var found = make([]*Command, 0, 1)
	var command *Command
	for i := range Commands {
		trialCommand := &Commands[i]
		// exact command name found - use that
		if trialCommand.Name == cmd {
			command = trialCommand
			break
		} else if strings.HasPrefix(trialCommand.Name, cmd) {
			found = append(found, trialCommand)
		}
	}
	if command == nil {
		switch len(found) {
		case 0:
			fs.Stats.Error()
			log.Fatalf("Unknown command %q", cmd)
		case 1:
			command = found[0]
		default:
			fs.Stats.Error()
			var names []string
			for _, cmd := range found {
				names = append(names, `"`+cmd.Name+`"`)
			}
			log.Fatalf("Not unique - matches multiple commands: %s", strings.Join(names, ", "))
		}
	}
	if command.Run == nil {
		syntaxError()
	}
	command.checkArgs(args)
	return command, args
}
Beispiel #7
0
func main() {
	opt := &Option{}
	pflag.BoolVarP(&opt.FileNameOnly, "filename-only", "f", false, "trim directory")
	pflag.BoolVarP(&opt.Replace, "replace", "r", false, "replace as javascript identifier")
	pflag.BoolVarP(&opt.Typing, "typing", "t", false, "output .d.ts for TypeScript")
	pflag.Parse()

	files := pflag.Args()
	if (opt.FileNameOnly || opt.Replace) && !checkFileUniq(files, opt) {
		fmt.Fprintln(os.Stderr, "Files should be uniq")
		os.Exit(1)
	}

	if opt.Typing {
		Typing(files, opt, os.Stdout)
		return
	}

	for _, fname := range files {
		if err := Translate(fname, os.Stdout, opt); err != nil {
			panic(err)
		}
	}
}
Beispiel #8
0
// Run the server.
func Run() {
	server.CloseOnAbort()

	flag.Usage = printUsage
	flag.Parse()

	if flagVersion != nil && *flagVersion {
		_, _, thisExeAbsPath := gotil.GetThisPathParts()
		fmt.Fprintf(os.Stdout, "Binary: %s\n", thisExeAbsPath)
		fmt.Fprint(os.Stdout, api.Version.String())
		os.Exit(0)
	}

	if flagEnv != nil && *flagEnv {
		for _, v := range os.Environ() {
			fmt.Fprintf(os.Stdout, "%s\n", v)
		}
		os.Exit(0)
	}

	// if a config is specified then do not care about any other options
	if flagConfig != nil && gotil.FileExists(*flagConfig) {

		config = gofigCore.New()

		if err := config.ReadConfigFile(*flagConfig); err != nil {
			fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
			os.Exit(1)
		}

		if flagPrintConfig != nil && *flagPrintConfig {
			jstr, err := config.ToJSON()
			if err != nil {
				fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
				os.Exit(1)
			}
			fmt.Fprintln(os.Stdout, jstr)
			os.Exit(0)
		}

		s, errs, err := server.Serve(nil, config)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
			os.Exit(1)
		}

		err = <-errs
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
			os.Exit(1)
		}

		s.Close()
		os.Exit(0)
	}

	cfg, err := apiconfig.NewConfig()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
		os.Exit(1)
	}

	config = cfg
	for _, fs := range config.FlagSets() {
		flag.CommandLine.AddFlagSet(fs)
	}

	if flagHelp != nil && *flagHelp {
		flag.Usage()
	}

	if len(flag.Args()) == 0 {
		flag.Usage()
	}

	if flagHost != nil {
		os.Setenv("LIBSTORAGE_HOST", *flagHost)
	}

	if flagLogLvl != nil {
		os.Setenv("LIBSTORAGE_LOGGING_LEVEL", *flagLogLvl)
	}

	if lvl, err := log.ParseLevel(
		config.GetString(apitypes.ConfigLogLevel)); err == nil {
		log.SetLevel(lvl)
	}

	if flagPrintConfig != nil && *flagPrintConfig {
		jstr, err := config.ToJSON()
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
			os.Exit(1)
		}
		fmt.Fprintln(os.Stdout, jstr)
		os.Exit(0)
	}

	buf := &bytes.Buffer{}
	fmt.Fprintf(buf, "libstorage:\n  server:\n    services:\n")
	for _, ds := range flag.Args() {
		dsp := strings.Split(ds, ":")
		dn := dsp[0]
		sn := dsp[0]
		if len(dsp) > 1 {
			sn = dsp[1]
		}
		fmt.Fprintf(buf, "      %s:\n        driver: %s\n", sn, dn)
	}
	if err := config.ReadConfig(buf); err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
		os.Exit(1)
	}

	server.CloseOnAbort()

	_, errs, err := server.Serve(nil, config)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
		os.Exit(1)
	}

	<-errs
}
Beispiel #9
0
		Help: `
        Enter an interactive configuration session.`,
		Run: func(fdst, fsrc fs.Fs) error {
			fs.EditConfig()
			return nil
		},
		NoStats: true,
	},
	{
		Name: "authorize",
		Help: `
        Remote authorization. Used to authorize a remote or headless
        rclone from a machine with a browser - use as instructed by
        rclone config.`,
		Run: func(fdst, fsrc fs.Fs) error {
			fs.Authorize(pflag.Args()[1:])
			return nil
		},
		NoStats: true,
		MinArgs: 1,
		MaxArgs: 3,
	},
	{
		Name: "help",
		Help: `
        This help.`,
		NoStats: true,
	},
}

// syntaxError prints the syntax