<commit_before>package main

import (
	dontlog "log"
	"net"
	"os"
	"os/signal"
	"sync"
	"time"

	"gopkg.in/alecthomas/kingpin.v2"

	"github.com/subparlabs/bonjourno/log"
	"github.com/subparlabs/bonjourno/service"
)

var (
	// Input methods
	say   = kingpin.Flag("say", "Create a share with this text").String()
	watch = kingpin.Flag("watch", "Periodically update with the first line of this file").ExistingFile()
	file  = kingpin.Flag("file", "Rotate through lines in this file").ExistingFile()

	// Message options
	interval = kingpin.Flag("interval", "Update interval for multiple strings (not watch, for ex), like 1s or 5m").Short('i').Default("1m").Duration()
	prefix   = kingpin.Flag("prefix", "Prefix all messages with this string").String()

	host = kingpin.Flag("host", "Host to broadast for the service").String()
	port = kingpin.Flag("port", "Port to broadast for the service").Int()
)

func main() {
	kingpin.Parse()

	inputStream, err := buildStream()
	if err != nil || inputStream == nil {
		kingpin.FatalUsage("Failed to create input stream: " + err.Error())
	}

	// Disable regular logging, cuz bonjour logs an error that isn't, and it's
	// confusing.
	dontlog.SetOutput(NullWriter{})

	if *host == "" {
		*host = getLocalIP()
	}
	if *port == 0 {
		// TODO: run a server that sends the same text on TCP conn
		*port = 45897
	}

	serv, err := service.New(*host, *port, *prefix)
	if err != nil || serv == nil {
		log.Panic("Failed to create service", "err", err)
	}

	// Watch for signal to clean up before we exit
	signals := make(chan os.Signal, 1)
	signal.Notify(signals, os.Interrupt, os.Kill)

	// Periodically update
	ticker := time.Tick(time.Second * 1)

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()

		// Init with something to say before waiting for the next interval
		serv.Say(inputStream.Get())

		for {
			select {
			case <-ticker:
				serv.Say(inputStream.Get())
			case <-signals:
				log.Info("Shutting down")
				serv.Stop()
				log.Info("Stopped service")
				return
			}
		}
	}()

	wg.Wait()
}

func buildStream() (service.InputStream, error) {
	var streams []service.InputStream

	if *watch != "" {
		if stream, err := service.NewFileWatcher(*watch); err != nil {
			return nil, err
		} else {
			streams = append(streams, stream)
		}
	}
	if *file != "" {
		if stream, err := service.NewFileLines(*file, *interval); err != nil {
			return nil, err
		} else {
			streams = append(streams, stream)
		}
	}
	if *say != "" {
		if stream, err := service.NewStaticText(*say); err != nil {
			return nil, err
		} else {
			streams = append(streams, stream)
		}
	}

	return service.NewPriorityMultistream(streams)
}

func getLocalIP() string {
	bestSoFar := "127.0.0.1"

	if addresses, err := net.InterfaceAddrs(); err == nil {<commit_msg>Read static msg as non-flag cmdline args<commit_after>package main

import (
	dontlog "log"
	"net"
	"os"
	"os/signal"
	"strings"
	"sync"
	"time"

	"gopkg.in/alecthomas/kingpin.v2"

	"github.com/subparlabs/bonjourno/log"
	"github.com/subparlabs/bonjourno/service"
)

var (
	// Input methods
	say   = kingpin.Arg("static text", "Create a share with this text").Strings()
	watch = kingpin.Flag("watch", "Periodically update with the first line of this file").ExistingFile()
	file  = kingpin.Flag("file", "Rotate through lines in this file").ExistingFile()

	// Message options
	interval = kingpin.Flag("interval", "Update interval for multiple strings (not watch, for ex), like 1s or 5m").Short('i').Default("1m").Duration()
	prefix   = kingpin.Flag("prefix", "Prefix all messages with this string").String()

	host = kingpin.Flag("host", "Host to broadast for the service").String()
	port = kingpin.Flag("port", "Port to broadast for the service").Int()
)

func main() {
	kingpin.Parse()

	inputStream, err := buildStream()
	if err != nil || inputStream == nil {
		kingpin.FatalUsage("Failed to create input stream: " + err.Error())
	}

	// Disable regular logging, cuz bonjour logs an error that isn't, and it's
	// confusing.
	dontlog.SetOutput(NullWriter{})

	if *host == "" {
		*host = getLocalIP()
	}
	if *port == 0 {
		// TODO: run a server that sends the same text on TCP conn
		*port = 45897
	}

	serv, err := service.New(*host, *port, *prefix)
	if err != nil || serv == nil {
		log.Panic("Failed to create service", "err", err)
	}

	// Watch for signal to clean up before we exit
	signals := make(chan os.Signal, 1)
	signal.Notify(signals, os.Interrupt, os.Kill)

	// Periodically update
	ticker := time.Tick(time.Second * 1)

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()

		// Init with something to say before waiting for the next interval
		serv.Say(inputStream.Get())

		for {
			select {
			case <-ticker:
				serv.Say(inputStream.Get())
			case <-signals:
				log.Info("Shutting down")
				serv.Stop()
				log.Info("Stopped service")
				return
			}
		}
	}()

	wg.Wait()
}

func buildStream() (service.InputStream, error) {
	var streams []service.InputStream

	if *watch != "" {
		if stream, err := service.NewFileWatcher(*watch); err != nil {
			return nil, err
		} else {
			streams = append(streams, stream)
		}
	}
	if *file != "" {
		if stream, err := service.NewFileLines(*file, *interval); err != nil {
			return nil, err
		} else {
			streams = append(streams, stream)
		}
	}
	if len(*say) > 0 {
		if stream, err := service.NewStaticText(strings.Join(*say, " ")); err != nil {
			return nil, err
		} else {
			streams = append(streams, stream)
		}
	}

	return service.NewPriorityMultistream(streams)
}

func getLocalIP() string {
	bestSoFar := "127.0.0.1"

	if addresses, err := net.InterfaceAddrs(); err == nil {
{'fraction_non_alphanumeric': 0.15182688896771904, 'fraction_numerical': 0.006917346576800284, 'mean_word_length': 3.54758064516129, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '148226', 'n_tokens_mistral': 2128, 'n_tokens_neox': 1938, 'n_words': 555}