Ejemplo n.º 1
0
Archivo: main.go Proyecto: vuleetu/misc
func main() {
	getopt.Parse()
	if *help {
		getopt.Usage()
		return
	}

	if *sock == "" || *cmd == "" {
		getopt.Usage()
		return
	}

	conn, err := net.Dial("unix", *sock)
	if err != nil {
		log.Fatalln(err)
	}

	var buf bytes.Buffer
	err = binary.Write(&buf, binary.BigEndian, uint32(len(*cmd)))
	if err != nil {
		log.Fatalln(err)
	}
	buf.WriteString(*cmd)
	_, err = conn.Write(buf.Bytes())
	if err != nil {
		log.Fatalln(err)
	}

	data, err := ioutil.ReadAll(conn)
	if err != nil {
		log.Println("error is", err)
	}

	fmt.Println(string(data))
}
Ejemplo n.º 2
0
func main() {
	var (
		debug       bool
		justVersion bool
		c           = proxy.Config{ListenAddr: defaultListenAddr}
	)

	c.Version = version
	getopt.BoolVarLong(&debug, "debug", 'd', "log debugging information")
	getopt.BoolVarLong(&justVersion, "version", 0, "print version and exit")
	getopt.StringVar(&c.ListenAddr, 'H', fmt.Sprintf("address on which to listen (default %s)", defaultListenAddr))
	getopt.BoolVarLong(&c.NoDefaultIPAM, "no-default-ipam", 0, "do not automatically allocate addresses for containers without a WEAVE_CIDR")
	getopt.StringVarLong(&c.TLSConfig.CACert, "tlscacert", 0, "Trust certs signed only by this CA")
	getopt.StringVarLong(&c.TLSConfig.Cert, "tlscert", 0, "Path to TLS certificate file")
	getopt.BoolVarLong(&c.TLSConfig.Enabled, "tls", 0, "Use TLS; implied by --tlsverify")
	getopt.StringVarLong(&c.TLSConfig.Key, "tlskey", 0, "Path to TLS key file")
	getopt.BoolVarLong(&c.TLSConfig.Verify, "tlsverify", 0, "Use TLS and verify the remote")
	getopt.BoolVarLong(&c.WithDNS, "with-dns", 'w', "instruct created containers to always use weaveDNS as their nameserver")
	getopt.BoolVarLong(&c.WithoutDNS, "without-dns", 0, "instruct created containers to never use weaveDNS as their nameserver")
	getopt.Parse()

	if justVersion {
		fmt.Printf("weave proxy %s\n", version)
		os.Exit(0)
	}

	if c.WithDNS && c.WithoutDNS {
		Error.Fatalf("Cannot use both '--with-dns' and '--without-dns' flags")
	}

	if debug {
		InitDefaultLogging(true)
	}

	Info.Println("weave proxy", version)
	Info.Println("Command line arguments:", strings.Join(os.Args[1:], " "))

	protoAddrParts := strings.SplitN(c.ListenAddr, "://", 2)
	if len(protoAddrParts) == 2 {
		if protoAddrParts[0] != "tcp" {
			Error.Fatalf("Invalid protocol format: %q", protoAddrParts[0])
		}
		c.ListenAddr = protoAddrParts[1]
	} else {
		c.ListenAddr = protoAddrParts[0]
	}

	p, err := proxy.NewProxy(c)
	if err != nil {
		Error.Fatalf("Could not start proxy: %s", err)
	}

	if err := p.ListenAndServe(); err != nil {
		Error.Fatalf("Could not listen on %s: %s", p.ListenAddr, err)
	}
}
Ejemplo n.º 3
0
func main() {
	var err error
	var dumpPath string
	var noHeaders bool
	var printRow_arg printRow_arg
	var channelsNum int

	getopt.StringVar(&dumpPath, 'i', "dump-path")
	getopt.StringVar(&printRow_arg.outputPath, 'o', "output-path").SetOptional()
	getopt.BoolVar(&noHeaders, 'n', "no-headers").SetOptional()
	getopt.IntVar(&channelsNum, 'c', "force-channels-num").SetOptional()
	getopt.BoolVar(&printRow_arg.binaryOutput, 'b', "binary-output").SetOptional()
	getopt.BoolVar(&printRow_arg.insertParseTime, 't', "insert-parse-time").SetOptional()

	getopt.Parse()
	if getopt.NArgs() > 0 || dumpPath == "" {
		getopt.Usage()
		os.Exit(-2)
	}
	switch printRow_arg.outputPath {
	/*		case "":
			now              := time.Now()
			year, month, day := now.Date()
			hour, min,   sec := now.Clock()
			printRow_arg.outputPath = fmt.Sprintf("%v_%v-%02v-%02v_%02v:%02v:%02v.csv", h.DeviceName, year, int(month), day, hour, min, sec)
			break*/
	case "", "-":
		printRow_arg.outputPath = "/dev/stdout"
		printRow_arg.outputFile = os.Stdout
		break
	default:
		printRow_arg.outputFile, err = os.Open(printRow_arg.outputPath)
		panic(fmt.Errorf("Not supported yet"))
	}

	if err != nil {
		fmt.Printf("Cannot open output file: %v", err.Error())
		os.Exit(-1)
	}
	//if (printRow_arg.binaryOutput) {
	//	err = binary.Write(printRow_arg.outputFile, binary.LittleEndian, printRow_arg)
	//}

	err = voltloggerParser.ParseVoltloggerDump(dumpPath, noHeaders, channelsNum, handleHeader, printRow, &printRow_arg)
	if err != nil {
		fmt.Printf("Cannot parse the dump: %v\n", err.Error())
		os.Exit(-1)
	}
	printRow_arg.outputFile.Close()

	fmt.Printf("%v %v\n", dumpPath, printRow_arg)
}
Ejemplo n.º 4
0
func main() {
	var (
		quiet = getopt.BoolLong("quiet", 'q', "Silence output")
		help  = getopt.BoolLong("help", 'h', "Help")
	)

	getopt.Parse()

	if *help {
		getopt.Usage()
		os.Exit(0)
	}

	output := new(bytes.Buffer)
	cmd := exec.Command("git", "rev-parse", "--show-toplevel")
	cmd.Stdout = output
	if err := cmd.Run(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	hookPath := path.Join(strings.TrimSpace(output.String()), ".git", "hooks", "pre-commit")

	hookFile, err := os.OpenFile(hookPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer hookFile.Close()

	if _, err = hookFile.WriteString(hook); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if !*quiet {
		fmt.Printf("git-duet-install-hook: Installed hook to %s\n", hookPath)
	}
}
Ejemplo n.º 5
0
func main() {
	getopt.HelpColumn = 35
	getopt.DisplayWidth = 120
	getopt.SetParameters("")
	getopt.Parse()

	if *bOptVer {
		printVersion()
	}

	if *bOptHelp {
		printHelp()
	}

	// --------------------------------------------------
	// Load System Configuration
	// --------------------------------------------------

	var syscfg config.ServerConfigType
	syscfg.LoadConfig(*sOptConfigFilename)

	// --------------------------------------------------
	// Setup Logging File
	// --------------------------------------------------
	// TODO
	// Need to make the directory if it does not already exist
	// To do this, we need to split the filename from the directory, we will want to only
	// take the last bit in case there is multiple directories /etc/foo/bar/stuff.log

	// Only enable logging to a file if it is turned on in the configuration file
	if syscfg.Logging.Enabled == true {
		logFile, err := os.OpenFile(syscfg.Logging.LogFileFullPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
		if err != nil {
			log.Fatalf("error opening file: %v", err)
		}
		defer logFile.Close()

		log.SetOutput(logFile)
	}

	// --------------------------------------------------
	// Setup Directory Path Handlers
	// --------------------------------------------------
	// Make sure there is a directory path defined in the configuration file
	// for each service we want to listen on.

	log.Println("Starting FreeTAXII Server")
	serviceCounter := 0

	// --------------------------------------------------
	// Setup Server Object for a listeners
	// --------------------------------------------------

	var taxiiServerObject taxiiserver.ServerType
	taxiiServerObject.SysConfig = &syscfg

	// TODO need to add the ability to signal this process and change the flag
	// from false to true so that it will be reloaded.
	taxiiServerObject.ReloadServices = true
	if syscfg.Logging.LogLevel >= 3 {
		log.Println("DEBUG-3: Setting reload services to true")
	}

	// --------------------------------------------------
	// Setup Discovery Server
	// --------------------------------------------------

	if syscfg.Services.Discovery != "" {
		log.Println("Starting TAXII Discovery services at:", syscfg.Services.Discovery)
		http.HandleFunc(syscfg.Services.Discovery, taxiiServerObject.DiscoveryServerHandler)
		serviceCounter++
	}

	// --------------------------------------------------
	// Setup Collection Server
	// --------------------------------------------------

	if syscfg.Services.Collection != "" {
		log.Println("Starting TAXII Collection services at:", syscfg.Services.Collection)
		http.HandleFunc(syscfg.Services.Collection, taxiiServerObject.CollectionServerHandler)
		serviceCounter++
	}

	// --------------------------------------------------
	// Setup Poll Server
	// --------------------------------------------------

	if syscfg.Services.Poll != "" {
		log.Println("Starting TAXII Poll services at:", syscfg.Services.Poll)
		http.HandleFunc(syscfg.Services.Poll, taxiiServerObject.PollServerHandler)
		serviceCounter++
	}

	// --------------------------------------------------
	// Setup Admin Server
	// --------------------------------------------------

	if syscfg.Services.Admin != "" {
		log.Println("Starting TAXII Admin services at:", syscfg.Services.Admin)
		http.HandleFunc(syscfg.Services.Admin, taxiiServerObject.AdminServerHandler)
		//serviceCounter++  Do not count this service in the list
	}

	// --------------------------------------------------
	// Fail if no services are running
	// --------------------------------------------------

	if serviceCounter == 0 {
		log.Fatalln("No TAXII services defined")
	}

	// --------------------------------------------------
	// Listen for Incoming Connections
	// --------------------------------------------------

	// TODO - Need to verify the list address is a valid IPv4 address and port
	// combination.
	if syscfg.System.Listen != "" {
		http.ListenAndServe(syscfg.System.Listen, nil)
	} else {
		log.Fatalln("The listen directive is missing from the configuration file")
	}

}
Ejemplo n.º 6
0
//
//
//
// --------------------------------------------------------------------------------
// Function Main
// --------------------------------------------------------------------------------
func main() {
	getopt.HelpColumn = 26
	getopt.SetParameters("")
	getopt.Parse()
	checkCommandLineOptions()

	iHeadCount := *iOptHead

	// // Figure out if there is a change needed for the date of each packet.  We will
	// // compute the difference between what is in the first packet and what was passed
	// // in via the command line arguments.
	// pcapStartTimestamp := getFirstPacketTimestamp(*sOptPcapSrcFilename)

	// // Parse layer 2 addresses
	// userSuppliedMacAddress := parseSuppliedLayer2Address(*sOptMacAddress)

	// // Parse layer 3 IPv4 address
	// userSuppliedIPv4Address := parseSuppliedLayer3IPv4Address(*sOptIPv4Address)

	//
	// Get a handle to the PCAP source file so we can loop through each packet and make
	// changes as needed.
	handle, err1 := pcap.OpenOffline(*sOptPcapSrcFilename)
	if err1 != nil {
		fmt.Println(err1)
		os.Exit(0)
	}
	packetSource := gopacket.NewPacketSource(handle, handle.LinkType())

	// -------------------------------------------------------------------------
	// Define counters for status
	// -------------------------------------------------------------------------
	iTotalPacketCounter := 0
	i802dot1QCounter := 0
	i802dot1QinQCounter := 0

	// -------------------------------------------------------------------------
	// Loop through every packet and update them as needed writing the changes
	// out to a new file
	// -------------------------------------------------------------------------
	for packet := range packetSource.Packets() {

		if *iOptHead != 0 && iHeadCount == 0 {
			return
		}

		ts := packet.Metadata().CaptureInfo.Timestamp
		dstMacAddressFromPacket := packet.LinkLayer().LayerContents()[0:6]
		srcMacAddressFromPacket := packet.LinkLayer().LayerContents()[6:12]

		sDstMacAddress := makePrettyMacAddress(dstMacAddressFromPacket)
		sSrcMacAddress := makePrettyMacAddress(srcMacAddressFromPacket)

		if *sOptMacAddress != "" {
			if sDstMacAddress != *sOptMacAddress && sSrcMacAddress != *sOptMacAddress {
				continue
			}
		}

		i802dot1QOffset := 0
		// ---------------------------------------------------------------------
		// Look for an 802.1Q frames
		// ---------------------------------------------------------------------
		if packet.LinkLayer().LayerContents()[12] == 81 && packet.LinkLayer().LayerContents()[13] == 0 {
			if iDebug == 1 {
				fmt.Println("DEBUG: Found an 802.1Q packet")
			}
			i802dot1QOffset = 4
			i802dot1QCounter++
		}

		// ---------------------------------------------------------------------
		// Look for an 802.1QinQ frame
		// ---------------------------------------------------------------------
		if packet.LinkLayer().LayerContents()[12] == 88 && packet.LinkLayer().LayerContents()[13] == 168 {
			if iDebug == 1 {
				fmt.Println("DEBUG: Found an 802.1QinQ packet")
			}
			i802dot1QOffset = 8
			i802dot1QinQCounter++
		}

		iEthType1 := 12 + i802dot1QOffset
		iEthType2 := 13 + i802dot1QOffset
		if packet.LinkLayer().LayerContents()[iEthType1] == 8 && packet.LinkLayer().LayerContents()[iEthType2] == 0 && packet.NetworkLayer().LayerContents()[0] == 69 {

			// Define the byte offsets for the data we are looking for
			iLayer3SrcIPStart := 12 + i802dot1QOffset
			iLayer3SrcIPEnd := iLayer3SrcIPStart + 4
			iLayer3DstIPStart := 16 + i802dot1QOffset
			iLayer3DstIPEnd := iLayer3DstIPStart + 4

			srcIPv4AddressFromPacket := packet.NetworkLayer().LayerContents()[iLayer3SrcIPStart:iLayer3SrcIPEnd]
			dstIPv4AddressFromPacket := packet.NetworkLayer().LayerContents()[iLayer3DstIPStart:iLayer3DstIPEnd]

			fmt.Print(ts, " - ", sSrcMacAddress)
			fmt.Printf(" - %-15s", makePrettyIPAddress(srcIPv4AddressFromPacket))
			fmt.Print(" > ", sDstMacAddress)
			fmt.Printf(" - %-15s\n", makePrettyIPAddress(dstIPv4AddressFromPacket))

		}

		// Write some output to the screen so users know we are doing something
		iTotalPacketCounter++

		if *iOptHead != 0 {
			iHeadCount--
		}

	} // End loop through every packet

	fmt.Println("\nTotal number of packets processed:", iTotalPacketCounter)
	fmt.Println("Total number of 802.1Q packets processed:", i802dot1QCounter)
	fmt.Println("Total number of 802.1QinQ packets processed:", i802dot1QinQCounter)
} // main()
Ejemplo n.º 7
0
func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	// var needle string
	// var filepattern string
	var distance int
	var patternFile string
	var help bool
	var verbose bool
	var simpleoutput bool

	getopt.IntVarLong(&distance, "edit", 'e', "Compute the approximate matching", "max_dist")
	getopt.StringVarLong(&patternFile, "pattern", 'p', "Use line-break separated patterns from a file", "filepath")
	getopt.StringVarLong(&cpuprofile, "cpuprofile", 0, "Write cpuprofile file", "path")
	getopt.StringVarLong(&memprofile, "memprofile", 0, "Write memprofile file", "path")
	getopt.BoolVarLong(&help, "help", 'h', "Shows this message")
	getopt.BoolVarLong(&verbose, "verbose", 'v', "Show log messages")
	getopt.BoolVarLong(&simpleoutput, "simple", 's', "Show simple output")
	getopt.SetProgram("pmt")
	getopt.SetParameters("needle [haystack ...]")
	getopt.SetUsage(func() {
		getopt.PrintUsage(os.Stderr)
		fmt.Fprintf(os.Stderr, "needle - only if -p was not used\n")
		fmt.Fprint(os.Stderr, "haystack\n")
	})
	getopt.Parse()

	if cpuprofile != "" {
		f, err := os.Create(cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if help {
		getopt.Usage()
		return
	}

	var patterns []string
	var files []string

	if patternFile == "" {
		// if(len(getopt.Args() )
		if getopt.NArgs() < 1 {
			fmt.Fprintf(os.Stderr, "Needle is missing!\n")
			getopt.Usage()
			os.Exit(1)
		}
		patterns = getopt.Args()[:1]
		files = getopt.Args()[1:]

	} else {
		var err error
		patterns, err = readLinesFromFile(patternFile)
		if err != nil {
			log.Fatal(err)
		}
		files = getopt.Args()
	}

	if verbose {
		log.Printf("%v %v %v\n", patterns, files, distance)
	}

	fileset := findFilesMatch(files)

	if distance == 0 {
		if len(patterns) == 1 {
			matcher := streammatch.NewKMP([]byte(patterns[0]))
			for fp, _ := range fileset {
				file, err := os.Open(fp)
				if err != nil {
					log.Fatal(err)
				}
				matches, err := processSingleExactMatcher(file, matcher, true)

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

				if !simpleoutput {
					printMatches(fp, file, patterns, matches, distance)
					if len(matches) > 0 {
						fmt.Println("###")
					}
				} else {
					printSimpleMatches(fp, file, patterns, matches)
				}

			}
		} else if len(patterns) > 1 {
			bpatterns := make([][]byte, len(patterns))
			for i, pattern := range patterns {
				bpatterns[i] = []byte(pattern)
			}
			matcher := streammatch.NewAhoCorasick(bpatterns)
			for fp, _ := range fileset {
				file, err := os.Open(fp)
				if err != nil {
					log.Fatal(err)
				}
				matches, err := processMultiExactMatcher(file, matcher)

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

				if !simpleoutput {
					printMatches(fp, file, patterns, matches, distance)
					if len(matches) > 0 {
						fmt.Println("###")
					}
				} else {
					printSimpleMatches(fp, file, patterns, matches)
				}

			}
		}
	} else {
		matchers := make([]streammatch.Matcher, 0, len(patterns))
		for i := 0; i < len(patterns); i++ {
			matchers = append(matchers, streammatch.NewSellers([]byte(patterns[i]), distance))
		}

		for fp, _ := range fileset {
			file, err := os.Open(fp)
			if err != nil {
				log.Fatal(err)
			}

			bufreader := bufio.NewReaderSize(file, defaultBufSize)
			allmatches := make([]matchRecord, 0, 2)
			for _, matcher := range matchers {
				_, err := file.Seek(0, 0)
				if err != nil {
					log.Fatal(err)
				}
				bufreader.Reset(file)
				matches, err := processSingleExactMatcher(bufreader, matcher, false)
				if err != nil {
					log.Fatal(err)
				}
				allmatches = append(allmatches, matches...)
			}

			sort.Stable(matchRecordList(allmatches))

			if !simpleoutput {
				printMatches(fp, file, patterns, allmatches, distance)
				if len(allmatches) > 0 {
					fmt.Println("###")
				}
			} else {
				printSimpleMatches(fp, file, patterns, allmatches)
			}
		}
	}
	if memprofile != "" {
		f, err := os.Create(memprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.WriteHeapProfile(f)
		defer f.Close()
		return
	}
}
func main() {
	getopt.HelpColumn = 35
	getopt.DisplayWidth = 120
	getopt.SetParameters("")
	getopt.Parse()

	if *bOptVer {
		printVersion()
	}

	if *bOptHelp {
		printHelp()
	}

	// --------------------------------------------------
	// Load Configuration File
	// --------------------------------------------------

	var syscfg config.ServerConfigType
	syscfg.LoadConfig(*sOptConfigFilename)

	// --------------------------------------------------
	// Setup Logging File
	// --------------------------------------------------
	// TODO
	// Need to make the directory if it does not already exist
	// To do this, we need to split the filename from the directory, we will want to only
	// take the last bit in case there is multiple directories /etc/foo/bar/stuff.log

	// Only enable logging to a file if it is turned on in the configuration file
	if syscfg.Logging.Enabled == true {
		logFile, err := os.OpenFile(syscfg.Logging.LogFileFullPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
		if err != nil {
			log.Fatalf("error opening file: %v", err)
		}
		defer logFile.Close()

		log.SetOutput(logFile)
	}

	log.Println("Starting FreeTAXII Management")

	// --------------------------------------------------
	// Open connection to database
	// --------------------------------------------------
	filename := syscfg.System.DbFileFullPath
	db, err := sql.Open("sqlite3", filename)
	if err != nil {
		log.Fatalf("Unable to open file %s due to error %v", filename, err)
	}
	defer db.Close()

	if DebugLevel >= 3 {
		log.Println("DEBUG-3: Using the following database file", filename)
	}

	// --------------------------------------------------
	// Check for what to do
	// --------------------------------------------------
	if *bOptListCollection {
		listCollections(db)
	}
	if *bOptAddCollection {
		addCollection(db)
	}
	if *bOptDelCollection {
		delCollection(db)
	}

}
Ejemplo n.º 9
0
func main() {
	var (
		quiet  = getopt.BoolLong("quiet", 'q', "Silence output")
		global = getopt.BoolLong("global", 'g', "Change global config")
		help   = getopt.BoolLong("help", 'h', "Help")
	)

	getopt.Parse()

	if *help {
		getopt.Usage()
		os.Exit(0)
	}

	configuration, err := duet.NewConfiguration()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if getopt.NArgs() == 0 {
		gitConfig, err := duet.GetAuthorConfig(configuration.Namespace)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		author, err := gitConfig.GetAuthor()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		printAuthor(author)
		os.Exit(0)
	}

	gitConfig := &duet.GitConfig{
		Namespace: configuration.Namespace,
	}
	if configuration.Global || *global {
		gitConfig.Scope = duet.Global
	}

	pairs, err := duet.NewPairsFromFile(configuration.PairsFile, configuration.EmailLookup)
	if err != nil {
		fmt.Println(err)
		os.Exit(0)
	}

	author, err := pairs.ByInitials(getopt.Arg(0))
	if err != nil {
		fmt.Println(err)
		os.Exit(86)
	}

	if err = gitConfig.SetAuthor(author); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if err = gitConfig.ClearCommitter(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if !*quiet {
		printAuthor(author)
	}
}
Ejemplo n.º 10
0
Archivo: reg.go Proyecto: knz/reg
func main() {
	ifname := getopt.StringLong("input", 'i', "-", "Set input stream for supplies (default '-', stdin)", "FILE")
	ofname := getopt.StringLong("output", 'o', "-", "Set output stream for reports (default '-', stdout)", "FILE")
	tspec := getopt.StringLong("ticks", 't', "", "Set tick generator (produces tick events)", "SPEC")
	sspec := getopt.StringLong("steps", 's', "", "Set progress indicator (measures steps)", "SPEC")
	mspec := getopt.StringLong("monitor", 'm', "", "Set resource monitor (measures stuff)", "SPEC")
	aspec := getopt.StringLong("actuator", 'a', "", "Set actuator (triggered upon supply exhaustion)", "SPEC")
	gran := getopt.StringLong("granularity", 'g', "0", "Force tick granularity (default 0, disabled)", "N")
	thr := getopt.StringLong("periodic-output", 'p', "none", "Configure periodic output (default none)", "PER")
	help := getopt.BoolLong("help", 'h', "Print this help")
	ver := getopt.BoolLong("version", 0, "Report version number")

	getopt.SetParameters("")
	getopt.Parse()

	/*** -h / -v ***/
	if *help {
		getopt.Usage()
		fmt.Println("\nReport bugs to http://github.com/knz/reg/issues")
		os.Exit(0)
	}

	if *ver {
		fmt.Println("reg (REG-ulator)", version)
		os.Exit(0)
	}

	/*** -i / -o ***/

	var fin, fout *os.File
	var err error

	if *ifname == "-" {
		fin = os.Stdin
	} else {
		fin, err = os.Open(*ifname)
		Assert(err == nil, "-i :", err)
	}

	if *ofname == "-" {
		fout = os.Stdout
	} else {
		fout, err = os.OpenFile(*ofname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
		Assert(err == nil, "-o :", err)
	}

	/*** -g ***/

	g, err := strconv.ParseFloat(*gran, 64)
	Assert(err == nil, "-g :", err)
	Assert(g >= 0, "-g : granularity cannot be negative")

	/*** -p ***/

	throttle_type := reg.OUTPUT_EXPLICIT_ONLY
	throttle_period := float64(0)

	spec_type, spec_flags, spec_arg := split(*thr)
	switch spec_type {
	case "none":
		Assert(spec_flags == "" && spec_arg == "", "-p none does not accept flags/argument")
		throttle_type = reg.OUTPUT_EXPLICIT_ONLY
	case "flood":
		Assert(spec_flags == "" && spec_arg == "", "-p flood does not accept flags/argument")
		throttle_type = reg.OUTPUT_FLOOD
	case "ticks":
		throttle_type = reg.OUTPUT_THROTTLE_TICKS
		if spec_arg != "" {
			throttle_period, err = strconv.ParseFloat(spec_arg, 64)
			Assert(err == nil, "-p ticks :", err)
		}
		Assert(spec_flags == "", "-p ticks does not accept flags")
	case "steps":
		throttle_type = reg.OUTPUT_THROTTLE_STEPS
		if spec_arg != "" {
			throttle_period, err = strconv.ParseFloat(spec_arg, 64)
			Assert(err == nil, "-p steps :", err)
		}
		Assert(spec_flags == "", "-p steps does not accept flags")
	default:
		Assert(false, "invalid syntax for -p")
	}
	Assert(throttle_period >= 0, "-p : period cannot be negative")

	/*** -a ***/

	var a act.Actuator
	spec_type, spec_flags, spec_arg = split(*aspec)
	Assert(spec_flags == "", "-a does not accept flags")
	switch spec_type {
	case "discard":
		Assert(spec_arg == "", "-a discard does not accept argument")
		a = act.MakeDummyActuator()
	case "print":
		var af *os.File
		if spec_arg == "-" || spec_arg == "" {
			af = os.Stdout
		} else {
			af, err = os.OpenFile(spec_arg, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
			Assert(err == nil, "-a :", err)
		}
		a = act.MakePrinterActuator(af)
	case "proc":
		a = act.MakeCommandActuator(cmd.MakeInteractiveCommand(spec_arg))
	case "cmd":
		a = act.MakeCommandActuator(cmd.MakeOneShotCommand(spec_arg))
	default:
		Assert(false, "invalid -a, or -a not specified")
	}

	/*** -t ***/

	var ts ticks.Source
	spec_type, spec_flags, spec_arg = split(*tspec)
	switch spec_type {
	case "instant":
		Assert(spec_flags == "", "-t instant does not accept flags")
		v := float64(0)
		if spec_arg != "" {
			v, err = strconv.ParseFloat(spec_arg, 64)
			Assert(err == nil, "-t instant:", err)
		}
		ts = ticks.MakeDummySource(t.Ticks(v))
	case "time", "ptime", "cmd", "proc":
		stype, ok := flagconv(spec_flags)
		Assert(ok, "invalid flags for -t")
		switch spec_type {
		case "time", "ptime":
			d, err := time.ParseDuration(spec_arg)
			Assert(err == nil, "-t :", err)
			ts = ticks.MakeTimerSource(d, stype, spec_type == "ptype")
		case "cmd":
			ts = ticks.MakeCommandSource(cmd.MakeOneShotCommand(spec_arg), stype)
		case "proc":
			ts = ticks.MakeCommandSource(cmd.MakeInteractiveCommand(spec_arg), stype)
		}
	default:
		Assert(false, "invalid -t, or -t not specified")
	}

	/*** -s ***/

	var ss steps.Source
	spec_type, spec_flags, spec_arg = split(*sspec)
	switch spec_type {
	case "const":
		Assert(spec_flags == "", "-s dummy does not accept flags")
		v := float64(0)
		if spec_arg != "" {
			v, err = strconv.ParseFloat(spec_arg, 64)
			Assert(err == nil, "-s const:", err)
			Assert(v >= 0, "-s const: constant cannot be negative")
		}
		ss = steps.MakeDummySource(t.Steps(v))
	case "cmd", "proc":
		stype, ok := flagconv(spec_flags)
		Assert(ok, "invalid flags for -s")
		switch spec_type {
		case "cmd":
			ss = steps.MakeCommandSource(cmd.MakeOneShotCommand(spec_arg), stype)
		case "proc":
			ss = steps.MakeCommandSource(cmd.MakeInteractiveCommand(spec_arg), stype)
		}
	default:
		Assert(false, "invalid -s, or -s not specified")
	}

	/*** -m ***/

	var m sample.Sampler
	spec_type, spec_flags, spec_arg = split(*mspec)
	switch spec_type {
	case "const":
		Assert(spec_flags == "", "-m const does not accept flags")
		v := float64(0)
		if spec_arg != "" {
			v, err = strconv.ParseFloat(spec_arg, 64)
			Assert(err == nil, "-m const:", err)
		}
		m = sample.MakeDummySampler(t.Stuff(v))
	case "cmd", "proc":
		Assert(spec_flags == "", "-m cmd/proc does not accept flags")
		switch spec_type {
		case "cmd":
			m = sample.MakeCommandSampler(cmd.MakeOneShotCommand(spec_arg))
		case "proc":
			m = sample.MakeCommandSampler(cmd.MakeInteractiveCommand(spec_arg))
		}
	default:
		Assert(false, "invalid -m, or -m not specified")
	}

	d := reg.MakeDomain(ts, ss, a, m)
	d.Start(fin, fout, throttle_type, throttle_period, t.Ticks(g))
	d.Wait()
}
Ejemplo n.º 11
0
func main() {
	var (
		quiet  = getopt.BoolLong("quiet", 'q', "Silence output")
		global = getopt.BoolLong("global", 'g', "Change global config")
		help   = getopt.BoolLong("help", 'h', "Help")
	)

	getopt.Parse()

	if *help {
		getopt.Usage()
		os.Exit(0)
	}

	configuration, err := duet.NewConfiguration()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if getopt.NArgs() == 0 {
		gitConfig, err := duet.GetAuthorConfig(configuration.Namespace)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		author, err := gitConfig.GetAuthor()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		committer, err := gitConfig.GetCommitter()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		if committer == nil {
			committer = author
		}

		printAuthor(author)
		printCommitter(committer)
		os.Exit(0)
	}

	gitConfig := &duet.GitConfig{
		Namespace: configuration.Namespace,
	}
	if configuration.Global || *global {
		gitConfig.Scope = duet.Global
	}

	if getopt.NArgs() <= 2 {
		fmt.Println("must specify more than two sets of initials")
		os.Exit(1)
	}

	pairs, err := duet.NewPairsFromFile(configuration.PairsFile, configuration.EmailLookup)
	if err != nil {
		fmt.Println(err)
		os.Exit(0)
	}

	author, err := pairs.ByInitials(getopt.Arg(0))
	if err != nil {
		fmt.Println(err)
		os.Exit(86)
	}

	if err = gitConfig.SetAuthor(author); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	number_of_committers := getopt.NArgs() - 1
	committers := make([]*duet.Pair, number_of_committers)

	for i := 1; i < getopt.NArgs(); i++ {
		committer, err := pairs.ByInitials(getopt.Arg(i))
		if err == nil {
			committers[i-1] = committer
		} else {
			fmt.Println(err)
			os.Exit(1)
		}
	}

	committer := makeTeamCommitter(committers)

	if err = gitConfig.SetCommitter(committer); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if !*quiet {
		printAuthor(author)
		printCommitter(committer)
	}
}