示例#1
0
/*------------------------------------------------------------------------
 * void process_options();
 *
 * Processes the command-line options and sets the protocol parameters
 * as appropriate.
 *------------------------------------------------------------------------*/
func ProcessOptions() *Parameter {
	parameter := NewParameter()

	opt.BoolVarLong(&parameter.verbose, "verbose", 'v',
		"turns on verbose output mode, deafult off")
	opt.BoolVarLong(&parameter.transcript, "transcript", 't',
		"turns on transcript mode for statistics recording, deafult off")
	opt.BoolVarLong(&parameter.ipv6, "v6", '6',
		"operates using IPv6 instead of (not in addition to!) IPv4")
	deafultHelp := fmt.Sprintf(
		"specifies which TCP port on which to listen to incoming connections, default %d",
		tsunami.TS_TCP_PORT)
	port := opt.Uint16Long("port", 'p', tsunami.TS_TCP_PORT, deafultHelp)
	secret := opt.StringLong("secret", 's', tsunami.DEFAULT_SECRET,
		"specifies the shared secret for the client and server")

	deafultHelp = fmt.Sprintf(
		"specifies the desired size for UDP socket send buffer (in bytes), default %d",
		DEFAULT_UDP_BUFFER)
	buffer := opt.Uint32Long("buffer", 'b', DEFAULT_UDP_BUFFER, deafultHelp)

	deafultHelp = fmt.Sprintf(
		"specifies the timeout in seconds for disconnect after client heartbeat lost, default %d",
		DEFAULT_HEARTBEAT_TIMEOUT)
	hbtimeout := opt.Uint16Long("hbtimeout", 'h', DEFAULT_HEARTBEAT_TIMEOUT, deafultHelp)
	opt.StringVarLong(&parameter.client, "client", 'c',
		"specifies an alternate client IP or host where to send data")
	opt.StringVarLong(&parameter.finishhook, "finishhook", 'f',
		"run command on transfer completion, file name is appended automatically")
	opt.StringVarLong(&parameter.allhook, "allhook", 'a',
		"run command on 'get *' to produce a custom file list for client downloads")
	opt.Parse()

	parameter.tcp_port = *port
	parameter.secret = *secret
	parameter.udp_buffer = *buffer
	parameter.hb_timeout = *hbtimeout

	files := opt.Args()
	parameter.file_names = files
	parameter.file_sizes = make([]uint64, len(files))
	parameter.total_files = uint16(len(files))
	for i, v := range files {
		stat, err := os.Stat(v)
		if err != nil {
			fmt.Fprintln(os.Stderr, v, err)
		} else {
			size := stat.Size()
			parameter.file_sizes[i] = uint64(size)
			fmt.Fprintf(os.Stderr, " %3d)   %-20s  %d bytes\n", i+1, v, size)
		}
	}

	parameter.VerboseArg("")
	return parameter
}
示例#2
0
func GetOptions() ([]string, string) {

	var files []string

	// parse option, move outside main
	optDebug = getopt.Bool('d', "debug", "Display debug info")
	optEcho = getopt.Bool('e', "echo", "Display processing in stdout")
	optHelp := getopt.Bool('h', "help", "Help")
	optAll = getopt.Bool('a', "all", "Process all parameters")
	optVersion := getopt.BoolLong("version", 'v', "Show version, then exit.")
	optCfgfile := getopt.StringLong("config", 'c', fileconfig, "Name of the configuration file to use.")
	//	optCycleMesure := getopt.StringLong("cycle_mesure", 'm', "", "Name of cycle_mesure")
	optFiles := getopt.StringLong("files", 'f', "", "files to process ex: data/fr25*.cnv")

	// parse options line argument
	getopt.Parse()

	// process bloc when option is set
	if *optHelp {
		getopt.Usage()
		os.Exit(0)
	}
	if *optVersion {
		fmt.Println(cfg.Progname + ": v" + cfg.Progversion)
		fmt.Printf("Environnement variable OCEANO2OCEANSITES: %s\n", os.Getenv("OCEANO2OCEANSITES"))
		fmt.Printf("Environnement variable ROSCOP: %s\n", os.Getenv("ROSCOP"))
		fmt.Printf("Configuration file: %s\n", fileconfig)
		fmt.Printf("Code ROSCOP file: %s\n", cfg.Roscopfile)
		fmt.Printf("GOPATH: %s\n", os.Getenv("GOPATH"))
		fmt.Printf("GOBIN: %s\n", os.Getenv("GOBIN"))
		os.Exit(0)
	}
	if *optDebug {
		debug = os.Stdout
	}
	if *optEcho {
		echo = os.Stdout
	}

	if *optAll {
		prefixAll = "-all"
	}
	// get files list from argument line
	// Args returns the non-option arguments.
	// see https://code.google.com/p/getopt/source/browse/set.go#27
	if *optFiles == "" {
		files = getopt.Args()
	} else {
		files, _ = filepath.Glob(*optFiles)
	}
	// if no files supplied for arg list, test if files is empty
	if len(files) == 0 {
		getopt.Usage()
		fmt.Println("\nPlease, specify files to process or define --files options")
		os.Exit(0)
	}
	//	if *optCycleMesure != "" {
	//		fmt.Println(*optCycleMesure)
	//		nc.Attributes["cycle_mesure"] = *optCycleMesure
	//	}
	fmt.Fprintln(debug, "Arg files list: ", files)
	return files, *optCfgfile
}
示例#3
0
文件: yang.go 项目: padmini-m/goyang
func main() {
	format := "tree"
	formats := make([]string, 0, len(formatters))
	for k := range formatters {
		formats = append(formats, k)
	}
	sort.Strings(formats)

	var traceP string
	var help bool
	getopt.CommandLine.ListVarLong(&yang.Path, "path", 0, "comma separated list of directories to add to PATH")
	getopt.CommandLine.StringVarLong(&format, "format", 0, "format to display: "+strings.Join(formats, ", "))
	getopt.CommandLine.StringVarLong(&traceP, "trace", 0, "file to write trace into")
	getopt.CommandLine.BoolVarLong(&help, "help", '?', "display help")

	getopt.Parse()

	if traceP != "" {
		fp, err := os.Create(traceP)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		trace.Start(fp)
		stop = func(c int) { trace.Stop(); os.Exit(c) }
		defer func() { trace.Stop() }()
	}

	if help {
		getopt.CommandLine.PrintUsage(os.Stderr)
		fmt.Fprintf(os.Stderr, "\nFormats:\n")
		for _, fn := range formats {
			f := formatters[fn]
			fmt.Fprintf(os.Stderr, "    %s - %s\n", f.name, f.help)
		}
		stop(0)
	}

	if _, ok := formatters[format]; !ok {
		fmt.Fprintf(os.Stderr, "%s: invalid format.  Choices are %s\n", format, strings.Join(formats, ", "))
		stop(1)

	}

	files := getopt.Args()

	if len(files) > 0 && !strings.HasSuffix(files[0], ".yang") {
		e, errs := yang.GetModule(files[0], files[1:]...)
		exitIfError(errs)
		Write(os.Stdout, e)
		return
	}

	// Okay, either there are no arguments and we read stdin, or there
	// is one or more file names listed.  Read them in and display them.

	ms := yang.NewModules()

	if len(files) == 0 {
		data, err := ioutil.ReadAll(os.Stdin)
		if err == nil {
			err = ms.Parse(string(data), "<STDIN>")
		}
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			stop(1)
		}
	}

	for _, name := range files {
		if err := ms.Read(name); err != nil {
			fmt.Fprintln(os.Stderr, err)
			continue
		}
	}

	// Process the read files, exiting if any errors were found.
	exitIfError(ms.Process())

	// Keep track of the top level modules we read in.
	// Those are the only modules we want to print below.
	mods := map[string]*yang.Module{}
	var names []string

	for _, m := range ms.Modules {
		if mods[m.Name] == nil {
			mods[m.Name] = m
			names = append(names, m.Name)
		}
	}
	sort.Strings(names)
	entries := make([]*yang.Entry, len(names))
	for x, n := range names {
		entries[x] = yang.ToEntry(mods[n])
	}

	formatters[format].f(os.Stdout, entries)
}
示例#4
0
func main() {
	getopt.SetParameters("<root dir> <bucket name>")
	getopt.Parse()
	if *help {
		getopt.PrintUsage(os.Stdout)
		return
	}

	args := getopt.Args()
	if len(args) != 2 {
		getopt.PrintUsage(os.Stderr)
		os.Exit(1)
	}

	rootDir := args[0]
	bucketName := args[1]

	resourcesMap := map[string]interface{}{}
	result := map[string]interface{}{
		"resource": map[string]interface{}{
			"aws_s3_bucket_object": resourcesMap,
		},
	}

	filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error reading %s: %s\n", path, err)
			// Skip stuff we can't read.
			return nil
		}

		relPath, err := filepath.Rel(rootDir, path)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed make %s relative: %s\n", path, err)
			return nil
		}

		path, err = filepath.EvalSymlinks(path)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to resolve symlink %s: %s\n", path, err)
			return nil
		}

		if info.IsDir() {
			// Don't need to create directories since they are implied
			// by the files within.
			return nil
		}

		for _, pattern := range *exclude {
			var toMatch []string
			if strings.ContainsRune(pattern, filepath.Separator) {
				toMatch = append(toMatch, relPath)
			} else {
				// If the pattern does not include a path separator
				// then we apply it to all segments of the path
				// individually.
				toMatch = strings.Split(relPath, string(filepath.Separator))
			}

			for _, matchPath := range toMatch {
				matched, _ := filepath.Match(pattern, matchPath)
				if matched {
					return nil
				}
			}
		}

		// We use the initial bytes of the file to infer a MIME type
		file, err := os.Open(path)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error opening %s: %s\n", path, err)
			return nil
		}
		hasher := sha1.New()
		fileBytes := make([]byte, 1024*1024)
		contentType := ""
		_, err = file.Read(fileBytes)
		// If we got back and error and it isn't the end of file then
		// skip it.  This does "something" with 0 length files.  It is
		// likely we should really be categorizing those based on file
		// extension.
		if err != nil && err != io.EOF {
			fmt.Fprintf(os.Stderr, "Error reading %s: %s\n", path, err)
			return nil
		}
		if strings.HasSuffix(relPath, ".svg") {
			// If we start to need a set of overrides for DetectContentType
			// then we need to find a different way to do this.
			contentType = "image/svg+xml"
		} else {
			contentType = http.DetectContentType(fileBytes)
		}

		// Resource name is a hash of the path, so it should stay consistent
		// for a given file path as long as the relative path to the target
		// directory is always the same across runs.
		hasher.Write([]byte(relPath))
		resourceName := fmt.Sprintf("%x", hasher.Sum(nil))

		resourcesMap[resourceName] = map[string]interface{}{
			"bucket":       bucketName,
			"key":          relPath,
			"source":       path,
			"etag":         fmt.Sprintf("${md5(file(%q))}", path),
			"content_type": contentType,
		}

		return nil
	})

	encoder := json.NewEncoder(os.Stdout)
	encoder.Encode(result)
}
示例#5
0
func main() {
	getopt.SetParameters("<root dir> <bucket name>")
	getopt.Parse()
	if *help {
		getopt.PrintUsage(os.Stdout)
		return
	}

	args := getopt.Args()
	if len(args) != 2 {
		getopt.PrintUsage(os.Stderr)
		os.Exit(1)
	}

	rootDir := args[0]
	bucketName := args[1]

	resourcesMap := map[string]interface{}{}
	result := map[string]interface{}{
		"resource": map[string]interface{}{
			"aws_s3_bucket_object": resourcesMap,
		},
	}

	filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error reading %s: %s\n", path, err)
			// Skip stuff we can't read.
			return nil
		}

		relPath, err := filepath.Rel(rootDir, path)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed make %s relative: %s\n", path, err)
			return nil
		}

		path, err = filepath.EvalSymlinks(path)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to resolve symlink %s: %s\n", path, err)
			return nil
		}

		if info.IsDir() {
			// Don't need to create directories since they are implied
			// by the files within.
			return nil
		}

		for _, pattern := range *exclude {
			var toMatch []string
			if strings.ContainsRune(pattern, filepath.Separator) {
				toMatch = append(toMatch, relPath)
			} else {
				// If the pattern does not include a path separator
				// then we apply it to all segments of the path
				// individually.
				toMatch = strings.Split(relPath, string(filepath.Separator))
			}

			for _, matchPath := range toMatch {
				matched, _ := filepath.Match(pattern, matchPath)
				if matched {
					return nil
				}
			}
		}

		resourceNameBytes := sha1.Sum([]byte(relPath))
		resourceName := fmt.Sprintf("%x", resourceNameBytes)

		resourcesMap[resourceName] = map[string]interface{}{
			"bucket": bucketName,
			"key":    relPath,
			"source": path,
		}

		return nil
	})

	encoder := json.NewEncoder(os.Stdout)
	encoder.Encode(result)
}
示例#6
0
文件: yang.go 项目: krousey/goyang
func main() {
	format := "tree"
	getopt.CommandLine.ListVarLong(&yang.Path, "path", 0, "comma separated list of directories to add to PATH")
	getopt.CommandLine.StringVarLong(&format, "format", 0, "format to display: tree, proto, types")

	getopt.Parse()
	files := getopt.Args()

	if len(files) > 0 && !strings.HasSuffix(files[0], ".yang") {
		e, errs := yang.GetModule(files[0], files[1:]...)
		exitIfError(errs)
		Write(os.Stdout, e)
		return
	}

	// Okay, either there are no arguments and we read stdin, or there
	// is one or more file names listed.  Read them in and display them.

	ms := yang.NewModules()

	if len(files) == 0 {
		data, err := ioutil.ReadAll(os.Stdin)
		if err == nil {
			err = ms.Parse(string(data), "<STDIN>")
		}
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
	}

	for _, name := range files {
		if err := ms.Read(name); err != nil {
			fmt.Fprintln(os.Stderr, err)
			continue
		}
	}

	// Keep track of the top level modules we read in.
	// Those are the only modules we want to print below.
	mods := map[string]*yang.Module{}
	var names []string

	for _, m := range ms.Modules {
		if mods[m.Name] == nil {
			mods[m.Name] = m
			names = append(names, m.Name)
		}
	}
	sort.Strings(names)

	// Print any errors found in the tree.  This will return false if
	// there were no errors.
	exitIfError(ms.Process())

	switch format {
	case "tree":
		for _, n := range names {
			Write(os.Stdout, yang.ToEntry(mods[n]))
		}
	case "proto":
		for _, n := range names {
			for _, e := range flatten(yang.ToEntry(mods[n])) {
				FormatNode(os.Stdout, e)
			}
		}
	case "types":
		types := Types{}
		for _, n := range names {
			types.AddEntry(yang.ToEntry(mods[n]))
		}

		for t := range types {
			YTPrint(os.Stdout, t)
		}
	default:
		fmt.Fprintf(os.Stderr, "unknown format: %s\n", format)
		os.Exit(1)
	}
}