<commit_before>package main

import (
	"bytes"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io"
	"log"
	"net/http"
	"net/url"
	"os"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/dustin/go-humanize"
	"github.com/influxdb/kapacitor"
	"github.com/influxdb/kapacitor/services/replay"
	"github.com/influxdb/kapacitor/services/task_store"
)

// These variables are populated via the Go linker.
var (
	version string
	commit  string
	branch  string
)

var mainFlags = flag.NewFlagSet("main", flag.ExitOnError)
var kapacitordURL = mainFlags.String("url", "http://localhost:9092", "the URL http(s)://host:port of the kapacitord server.")

var kapacitorEndpoint string

var l = log.New(os.Stderr, "[run] ", log.LstdFlags)

var usageStr = `
Usage: kapacitor [options] [command] [args]

Commands:

	record   record the result of a query or a snapshot of the current stream data.
	define   create/update a task.
	replay   replay a recording to a task.
	enable   enable and start running a task with live data.
	disable  stop running a task.
	reload   reload a running task with an updated task definition.
	push     publish a task definition to another Kapacitor instance. Not implemented yet.
	delete   delete a task or a recording.
	list     list information about tasks or recordings.
	show     display detailed information about a task.
	help     get help for a command.
	level    sets the logging level on the kapacitord server.
	version  displays the Kapacitor version info.

Options:
`

func usage() {
	fmt.Fprintln(os.Stderr, usageStr)
	mainFlags.PrintDefaults()
	os.Exit(1)
}

func main() {

	mainFlags.Parse(os.Args[1:])

	kapacitorEndpoint = *kapacitordURL + "/api/v1"

	args := mainFlags.Args()

	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "Error: Must pass a command.")
		usage()
	}

	command := args[0]
	args = args[1:]
	var commandF func(args []string) error
	var commandArgs []string
	switch command {
	case "help":
		commandArgs = args
		commandF = doHelp
	case "record":
		if len(args) == 0 {
			recordFlags.Usage()
			os.Exit(2)
		}
		recordFlags.Parse(args[1:])
		commandArgs = args[0:1]
		commandF = doRecord
	case "define":
		defineFlags.Parse(args)
		commandArgs = defineFlags.Args()
		commandF = doDefine
	case "replay":<commit_msg>use env var for kapacitor url<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io"
	"log"
	"net/http"
	"net/url"
	"os"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/dustin/go-humanize"
	"github.com/influxdb/kapacitor"
	"github.com/influxdb/kapacitor/services/replay"
	"github.com/influxdb/kapacitor/services/task_store"
)

// These variables are populated via the Go linker.
var (
	version string
	commit  string
	branch  string
)

var defaultURL = "http://localhost:9092"

var mainFlags = flag.NewFlagSet("main", flag.ExitOnError)
var kapacitordURL = mainFlags.String("url", "", "the URL http(s)://host:port of the kapacitord server. Defaults to the KAPACITOR_URL environment variable or "+defaultURL+" if not set.")

var kapacitorEndpoint string

var l = log.New(os.Stderr, "[run] ", log.LstdFlags)

var usageStr = `
Usage: kapacitor [options] [command] [args]

Commands:

	record   record the result of a query or a snapshot of the current stream data.
	define   create/update a task.
	replay   replay a recording to a task.
	enable   enable and start running a task with live data.
	disable  stop running a task.
	reload   reload a running task with an updated task definition.
	push     publish a task definition to another Kapacitor instance. Not implemented yet.
	delete   delete a task or a recording.
	list     list information about tasks or recordings.
	show     display detailed information about a task.
	help     get help for a command.
	level    sets the logging level on the kapacitord server.
	version  displays the Kapacitor version info.

Options:
`

func usage() {
	fmt.Fprintln(os.Stderr, usageStr)
	mainFlags.PrintDefaults()
	os.Exit(1)
}

func main() {

	mainFlags.Parse(os.Args[1:])

	url := defaultURL
	if urlEnv := os.Getenv("KAPACITOR_URL"); urlEnv != "" {
		url = urlEnv
	}
	if *kapacitordURL != "" {
		url = *kapacitordURL
	}

	kapacitorEndpoint = url + "/api/v1"

	args := mainFlags.Args()

	if len(args) == 0 {
		fmt.Fprintln(os.Stderr, "Error: Must pass a command.")
		usage()
	}

	command := args[0]
	args = args[1:]
	var commandF func(args []string) error
	var commandArgs []string
	switch command {
	case "help":
		commandArgs = args
		commandF = doHelp
	case "record":
		if len(args) == 0 {
			recordFlags.Usage()
			os.Exit(2)
		}
		recordFlags.Parse(args[1:])
		commandArgs = args[0:1]
		commandF = doRecord
	case "define":
		defineFlags.Parse(args)
		commandArgs = defineFlags.Args()
		commandF = doDefine
	case "replay":
{'fraction_non_alphanumeric': 0.11204540676897204, 'fraction_numerical': 0.006306495690561278, 'mean_word_length': 3.7771084337349397, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '6138065', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1653, 'n_words': 505}