Beispiel #1
0
func main() {
	_, err := docopt.Parse(Usage, nil, true, Version, false)
	if err != nil {
		log.Fatalf("[error] %s", err)
	}

	c, err := config.Read(os.Stdin)
	if err != nil {
		log.Fatalf("[error] parsing config: %s", err)
	}

	s := server.New(c)

	log.Printf("[info] starting sdns %s", Version)
	err = s.Start()
	if err != nil {
		log.Fatalf("[error] %s", err)
	}

	log.Printf("[info] binding on %s", s.Config.Bind)
	gracefully.Shutdown()

	log.Printf("[info] stopping")
	err = s.Stop()
	if err != nil {
		log.Fatalf("[error] %s", err)
	}

	log.Printf("[info] bye :)")
}
Beispiel #2
0
func main() {
	args, err := docopt.Parse(Usage, nil, true, Version, false)
	if err != nil {
		log.Fatal("[error] %s", err)
	}

	topic := args["--topic"].(string)
	addr := args["--address"].(string)
	nsqd := args["--nsqd-tcp-address"].(string)

	log.Printf("starting http_to_nsq %s", Version)
	log.Printf("--> binding to %s", addr)
	log.Printf("--> publishing to %s as topic %q", nsqd, topic)

	prod, err := nsq.NewProducer(nsqd, nsq.NewConfig())
	if err != nil {
		log.Fatal("[error] starting producer: %s", err)
	}

	server := &http_to_nsq.Server{
		Log:       log.New(os.Stderr, "", log.LstdFlags),
		Secret:    args["--secret"].(string),
		Topic:     "builds",
		Publisher: prod,
	}

	err = http.ListenAndServe(addr, server)
	if err != nil {
		log.Fatal("[error] binding: %s", err)
	}
}
Beispiel #3
0
func main() {
	args, err := docopt.Parse(Usage, nil, true, Version, false)
	log.Check(err)

	log.Info("starting system %s", Version)

	client, err := statsd.Dial(args["--statsd-address"].(string))
	log.Check(err)

	extended := args["--extended"].(bool)

	name := args["--name"].(string)
	if "hostname" == name {
		host, err := os.Hostname()
		log.Check(err)
		name = host
	}

	c := collector.New(namespace.New(client, name))
	c.Add(memory.New(interval(args, "--memory-interval"), extended))
	c.Add(cpu.New(interval(args, "--cpu-interval"), extended))
	c.Add(disk.New(interval(args, "--disk-interval")))

	c.Start()
	Shutdown()
	c.Stop()
}
Beispiel #4
0
func main() {
	args, err := docopt.Parse(usage, nil, true, version, true)
	if err != nil {
		cli.Fatalf("error parsing arguments: %s", err)
	}

	file := args["--config"].(string)
	c, err := config.New(file)
	if err != nil {
		cli.Fatalf("error loading configuration: %s", err)
	}

	switch {
	case args["help"].(bool):
		if name, ok := args["<task>"].(string); ok {
			cli.Help(c, name)
		} else {
			cli.List(c)
		}
	case args["variables"].(bool):
		cli.ListVariables(c)
	default:
		if name, ok := args["<task>"].(string); ok {
			cli.Run(c, name, args["<arg>"].([]string))
		} else {
			cli.List(c)
		}
	}
}
Beispiel #5
0
func main() {
	args, err := docopt.Parse(usage, nil, true, version, false)
	if err != nil {
		log.Fatalf("error: %s", err)
	}

	log.SetHandler(cli.Default)

	if l, err := log.ParseLevel(args["--log-level"].(string)); err == nil {
		log.SetLevel(l)
	}

	if args["help"].(bool) {
		showHelp(args["<topic>"])
		return
	}

	session := session.New(aws.NewConfig())

	project := &project.Project{
		Log:  log.Log,
		Path: ".",
	}

	if args["--dry-run"].(bool) {
		log.SetLevel(log.WarnLevel)
		project.Service = dryrun.New(session)
		project.Concurrency = 1
	} else {
		project.Service = lambda.New(session)
	}

	if dir, ok := args["--chdir"].(string); ok {
		if err := os.Chdir(dir); err != nil {
			log.Fatalf("error: %s", err)
		}
	}

	if err := project.Open(); err != nil {
		log.Fatalf("error opening project: %s", err)
	}

	switch {
	case args["list"].(bool):
		list(project)
	case args["deploy"].(bool):
		deploy(project, args["<name>"].([]string), args["--env"].([]string))
	case args["delete"].(bool):
		delete(project, args["<name>"].([]string), args["--yes"].(bool))
	case args["invoke"].(bool):
		invoke(project, args["<name>"].([]string), args["--verbose"].(bool), args["--async"].(bool))
	case args["rollback"].(bool):
		rollback(project, args["<name>"].([]string), args["<version>"])
	case args["build"].(bool):
		build(project, args["<name>"].([]string))
	case args["logs"].(bool):
		tail(project, args["<name>"].([]string), args["--filter"].(string))
	}
}
Beispiel #6
0
func run(argv []string, in io.Reader, out io.Writer) {
	arguments, err := docopt.Parse(usage, argv, true, version, false)
	check(err)

	indent, err := strconv.Atoi(arguments["--indentation"].(string))
	check(err)

	DiLaurentis(in, out, indent)
}
Beispiel #7
0
func main() {
	args, err := docopt.Parse(Usage, nil, true, Version, false)
	if err != nil {
		log.Fatalf("error: %s", err)
	}

	log.Printf("starting nsq_to_postgres version %s", Version)

	// Read config
	file := args["--config"].(string)
	b, err := ioutil.ReadFile(file)
	if err != nil {
		log.Fatalf("error reading config: %s", err)
	}

	// Unmarshal config
	config := new(Config)
	err = yaml.Unmarshal(b, config)
	if err != nil {
		log.Fatalf("error unmarshalling config: %s", err)
	}

	// Validate config
	err = config.Postgres.Validate()
	if err != nil {
		log.Fatalf("configuration error: %s", err)
	}

	// Apply nsq config
	c := queue.NewConsumer("", "nsq_to_postgres")

	for k, v := range config.Nsq {
		c.Set(k, v)
	}

	// Connect
	log.Printf("connecting to postgres")
	db, err := client.New(config.Postgres)
	if err != nil {
		log.Fatalf("error connecting: %s", err)
	}

	// Bootstrap with table
	err = db.Bootstrap()
	if err != nil {
		log.Printf("error bootstrapping: %s", err)
	}

	// Start consumer
	log.Printf("starting consumer")
	c.Start(handler.New(db))
	gracefully.Shutdown()
	log.Printf("stopping consumer")
	c.Stop()

	log.Printf("bye :)")
}
Beispiel #8
0
func main() {
	arguments, err := docopt.Parse(Usage, nil, true, Version, false)
	check(err)

	words := arguments["<word>"].([]string)

	for _, w := range words {
		Spell(w)
		fmt.Println()
	}
}
Beispiel #9
0
func main() {
	args, err := docopt.Parse(Usage, nil, true, Version, true)
	if err != nil {
		Fatalf("Failed to parse arguments: %s", err)
	}

	if args["--template-file"] == nil && args["--template-data"] == nil {
		fmt.Println(Usage)
		return
	}

	Run(
		args["<key=value>"].([]string),
		getPipedString(),
		getStringFromInterface(args["--template-file"]),
		getStringFromInterface(args["--template-data"]),
	)
}
Beispiel #10
0
func main() {
	args, err := docopt.Parse(usage, nil, true, version, false)

	if err != nil {
		log.Fatalf("error parsing arguments: %s", err)
	}

	file := args["<file>"].(string)
	c, err := config.New(file)
	if err != nil {
		log.Fatalf("error loading configuration: %s", err)
	}

	log.Printf("starting monitor")
	watch.NewMonitor(c).Start()
	gracefully.Shutdown()
	log.Printf("stopping monitor")
}
Beispiel #11
0
func main() {
	arguments, err := docopt.Parse(usage, nil, true, version, false)
	check(err)

	hnClient := hn.New()
	instapaperClient := instapaper.New(arguments["<username>"].(string), arguments["<password>"].(string))
	limit, err := strconv.Atoi(arguments["--limit"].(string))
	check(err)

	stories, err := hnClient.TopStories()
	check(err)

	s := semaphore.New(10)
	var wg sync.WaitGroup
	for i, id := range stories {
		if i >= limit {
			break
		}

		wg.Add(1)
		s.Acquire(1)

		go func(id int) {
			defer wg.Done()
			defer s.Release(1)

			story, err := hnClient.GetPost(id)
			check(err)
			if story.URL == nil {
				fmt.Println("Skipping", *story.Title)
				return
			}

			_, err = instapaperClient.Add(instapaper.AddParams{
				URL:   *story.URL,
				Title: story.Title,
			})
			check(err)
			fmt.Println("Saved", *story.Title)
		}(id)
	}
	wg.Wait()
}
Beispiel #12
0
func main() {
	arguments, err := docopt.Parse(Usage, nil, true, Version, false)
	check(err)

	cmd := arguments["<cmd>"].(string)
	args := arguments["<args>"].([]string)
	iterations := parseInt(arguments["--iterations"].(string))
	parallel := arguments["--parallel"].(bool)

	fmt.Println("Running", cmd, args, iterations, "times.")

	if parallel {
		executeParallel(iterations, cmd, args...)
	} else {
		executeSerial(iterations, cmd, args...)
	}

	fmt.Println()
	fmt.Println("Completed", iterations, "iterations without any errors.")
}
Beispiel #13
0
func main() {
	arguments, err := docopt.Parse(usage, nil, true, version, false)
	check(err)

	config := readConfig(arguments)

	f, err := os.Create("config.json")
	check(err)
	defer f.Close()

	b, err := json.Marshal(config)
	check(err)

	// Prettifying is not required, but makes it easier for humans to read.
	var buf bytes.Buffer
	err = json.Indent(&buf, b, "  ", "\t")
	check(err)
	_, err = buf.WriteTo(f)
	check(err)
}
Beispiel #14
0
func main() {
	usage := `9pmount

Usage:
  9pmount <mount_point> (--address <address>)

Options:
  -h --help                    Show this screen.
  --version                    Show version.
  --address                    Address of 9pserver`

	args, err := docopt.Parse(usage, nil, true, "9pmount 0.1", false)
	mount_point := args["<mount_point>"].(string)
	address = args["<address>"].(string)
	nfs := pathfs.NewPathNodeFs(&P9Fs{FileSystem: pathfs.NewDefaultFileSystem()}, nil)
	server, _, err := nodefs.MountRoot(mount_point, nfs.Root(), nil)
	if err != nil {
		log.Fatalf("Mount fail: %v\n", err)
	}
	server.Serve()
}
Beispiel #15
0
func main() {
	arguments, err := docopt.Parse(Usage, nil, true, Version, false)
	check(err)

	owner := arguments["<owner>"].(string)
	repo := arguments["<repo>"].(string)
	token := arguments["<token>"].(string)

	initializeClient(token)

	contributors := getContributors(owner, repo)
	names := getNames(contributors)

	percentFemale, percentMale := predictGenderStats(names)
	percentUnknown := (100 - percentFemale - percentMale)

	fmt.Println("\nContributors by Gender:")
	fmt.Printf("\n  - Female: %.2f%%\n", percentFemale)
	fmt.Printf("\n  - Male: %.2f%%\n", percentMale)
	fmt.Printf("\n  - Unknown: %.2f%%\n\n", percentUnknown)
}
Beispiel #16
0
func main() {
	args, err := docopt.Parse(usage, nil, true, version, false)
	if err != nil {
		log.Fatal(err)
	}

	region := args["--region"].(string)
	bucket := args["<bucket>"].(string)
	file := args["<file>"].(string)

	cfg := &ls.Config{
		Bucket: bucket,
		Region: region,
	}

	files, err := ls.List(file, cfg)
	if err != nil {
		log.Fatal(err)
	}

	listFiles(files)
}
Beispiel #17
0
func main() {
	args, err := docopt.Parse(usage, nil, true, version, true)
	if err != nil {
		fatalf("Failed to parse arguments: %v\n%v", args, err)
	}

	var file, dir string
	fileOrDir := resolvePath(args["<file-or-directory>"].(string))
	stat, err := os.Stat(fileOrDir)
	if os.IsNotExist(err) {
		fatalf("No such file or directory: %v\n%v", fileOrDir, err)
	}
	if stat.IsDir() {
		dir = fileOrDir
	} else {
		file = fileOrDir
		dir = filepath.Dir(file)
	}

	if args["--output-directory"] == nil {
		args["--output-directory"] = dir
	}
	if args["--schema-directory"] == nil {
		args["--schema-directory"] = dir
	}

	cli := &Cli{
		dir:            dir,
		file:           file,
		outputDir:      resolvePath(args["--output-directory"].(string)),
		schemaDir:      resolvePath(args["--schema-directory"].(string)),
		encoding:       args["--encoding"].(string),
		fixEncoding:    args["--fix-encoding"].(bool),
		noOutputFile:   args["--no-output-file"].(bool),
		outputSchema:   args["--output-schema"].(bool),
		skipValidation: args["--skip-validation"].(bool),
	}
	cli.run()
}
Beispiel #18
0
func main() {
	arguments, err := docopt.Parse(Usage, nil, true, "Anaytics Go CLI", false)
	check(err)

	writeKey := getOptionalString(arguments, "--writeKey")
	if writeKey == "" {
		writeKey = os.Getenv("SEGMENT_WRITE_KEY")
		if writeKey == "" {
			log.Fatal("either $SEGMENT_WRITE_KEY or --writeKey must be provided")
		}
	}

	client := analytics.New(writeKey)
	client.Size = 1
	client.Verbose = true

	if arguments["track"].(bool) {
		m := &analytics.Track{
			Event: arguments["<event>"].(string),
		}
		properties := getOptionalString(arguments, "--properties")
		if properties != "" {
			var parsedProperties map[string]interface{}
			err := json.Unmarshal([]byte(properties), &parsedProperties)
			check(err)
			m.Properties = parsedProperties
		}

		setCommonFields(m, arguments)

		check(client.Track(m))
	}

	if arguments["screen"].(bool) || arguments["page"].(bool) {
		m := &analytics.Page{
			Name: arguments["<name>"].(string),
		}
		/* Bug in Go library - page has traits not properties.
		properties := getOptionalString(arguments, "--properties")
		if properties != "" {
			var parsedProperties map[string]interface{}
			err := json.Unmarshal([]byte(properties), &parsedProperties)
			check(err)
			t.Properties = parsedProperties
		}
		*/

		setCommonFields(m, arguments)

		check(client.Page(m))
	}

	if arguments["identify"].(bool) {
		m := &analytics.Identify{}
		traits := getOptionalString(arguments, "--traits")
		if traits != "" {
			var parsedTraits map[string]interface{}
			err := json.Unmarshal([]byte(traits), &parsedTraits)
			check(err)
			m.Traits = parsedTraits
		}

		setCommonFields(m, arguments)

		check(client.Identify(m))
	}

	if arguments["group"].(bool) {
		m := &analytics.Group{
			GroupId: arguments["--groupId"].(string),
		}
		traits := getOptionalString(arguments, "--traits")
		if traits != "" {
			var parsedTraits map[string]interface{}
			err := json.Unmarshal([]byte(traits), &parsedTraits)
			check(err)
			m.Traits = parsedTraits
		}

		setCommonFields(m, arguments)

		check(client.Group(m))
	}

	if arguments["alias"].(bool) {
		m := &analytics.Alias{
			PreviousId: arguments["--previousId"].(string),
		}

		setCommonFields(m, arguments)

		check(client.Alias(m))
	}

	client.Close()
}
Beispiel #19
0
func main() {
	m, err := docopt.Parse(Usage, nil, true, Version, false)
	if err != nil {
		logrus.Fatal(err)
	}

	if m["--debug"].(bool) {
		logrus.SetLevel(logrus.DebugLevel)
	}

	if m["--json-log"].(bool) {
		logrus.SetFormatter(logrus_ecslogs.NewFormatter())
	}

	concurrency, err := strconv.Atoi(m["--concurrency"].(string))
	if err != nil {
		logrus.Fatal(err)
	}

	// Load and validate DB configuration.
	config := &mongodb.Config{
		Init:     m["--init"].(bool),
		Hostname: m["--hostname"].(string),
		Port:     m["--port"].(string),
		Username: m["--username"].(string),
		Password: m["--password"].(string),
		Database: m["--database"].(string),
	}

	_, err = govalidator.ValidateStruct(config)
	if err != nil {
		logrus.Error(err)
		return
	}

	// If in init mode, save list of collections to schema file. Users will then have to modify the
	// file and fill in fields they want to export to their Segment warehouse.
	fileName := m["--schema"].(string)
	if config.Init {
		mongodb.InitSchema(config, fileName)
		return
	}

	// Build Segment client and define publish function for when we scan over the collections.
	writeKey := m["--write-key"].(string)
	if writeKey == "" {
		logrus.Fatal("Write key is required when not in init mode.")
	}

	description, err := mongodb.ParseSchema(fileName)
	if err == io.EOF {
		logrus.Error("Empty schema, did you run `--init`?")
	} else if err != nil {
		logrus.Fatal("Unable to parse schema", err)
	}

	segmentClient := objects.New(writeKey)
	defer segmentClient.Close()
	setWrapperFunc := func(o *objects.Object) {
		err := segmentClient.Set(o)
		if err != nil {
			logrus.WithFields(logrus.Fields{"id": o.ID, "collection": o.Collection, "properties": o.Properties}).Warn(err)
		}
	}

	logrus.Info("[%v] Mongo source started with writeKey ", Version, writeKey)
	if err := mongodb.Run(config, description, concurrency, setWrapperFunc); err != nil {
		logrus.Error("mongodb source failed to complete", err)
		os.Exit(1)
	}
}
Beispiel #20
0
func Run(d driver.Driver) {
	app := &driver.Base{d}

	m, err := docopt.Parse(usage, nil, true, Version, false)
	if err != nil {
		logrus.Error(err)
		return
	}

	segmentClient := objects.New(m["--write-key"].(string))

	setWrapper := func(o *objects.Object) {
		if err := segmentClient.Set(o); err != nil {
			logrus.WithFields(logrus.Fields{"id": o.ID, "collection": o.Collection, "properties": o.Properties}).Warn(err)
		}
	}

	config := &domain.Config{
		Init:         m["--init"].(bool),
		Hostname:     m["--hostname"].(string),
		Port:         m["--port"].(string),
		Username:     m["--username"].(string),
		Password:     m["--password"].(string),
		Database:     m["--database"].(string),
		ExtraOptions: m["<extra-driver-options>"].([]string),
	}

	if m["--debug"].(bool) {
		logrus.SetLevel(logrus.DebugLevel)
	}

	concurrency, err := strconv.Atoi(m["--concurrency"].(string))
	if err != nil {
		logrus.Error(err)
		return
	}

	// Validate the configuration
	if _, err := govalidator.ValidateStruct(config); err != nil {
		logrus.Error(err)
		return
	}

	// Open the schema
	schemaFile, err := os.OpenFile(m["--schema"].(string), os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		logrus.Error(err)
		return
	}
	defer schemaFile.Close()

	if err := app.Driver.Init(config); err != nil {
		logrus.Error(err)
		return
	}

	// Initialize the source
	if config.Init {
		description, err := app.Driver.Describe()
		if err != nil {
			logrus.Error(err)
			return
		}
		if err := description.Save(schemaFile); err != nil {
			logrus.Error(err)
			return
		}

		schemaFile.Sync()
		logrus.Infof("Saved to `%s`", schemaFile.Name())
		return
	}

	description, err := domain.NewDescriptionFromReader(schemaFile)
	if err == io.EOF {
		logrus.Error("Empty schema, did you run `--init`?")
		return
	} else if err != nil {
		logrus.Error(err)
		return
	}

	sem := make(semaphore.Semaphore, concurrency)

	for table := range description.Iter() {
		sem.Acquire()
		go func(table *domain.Table) {
			defer sem.Release()
			logrus.WithFields(logrus.Fields{"table": table.TableName, "schema": table.SchemaName}).Info("Scan started")
			if err := app.ScanTable(table, setWrapper); err != nil {
				logrus.Error(err)
			}
			logrus.WithFields(logrus.Fields{"table": table.TableName, "schema": table.SchemaName}).Info("Scan finished")
		}(table)
	}

	sem.Wait()
	segmentClient.Close()

	// Log status
	for table := range description.Iter() {
		logrus.WithFields(logrus.Fields{"schema": table.SchemaName, "table": table.TableName, "count": table.State.ScannedRows}).Info("Sync Finished")
	}
}