<commit_before>package command

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/Sirupsen/logrus"
	"github.com/jessevdk/go-flags"
	"github.com/mitchellh/cli"
	"github.com/nerdalize/nerd/nerd/conf"
	"github.com/pkg/errors"
)

//TaskStartOpts describes command options
type TaskStartOpts struct {
	NerdOpts
}

//TaskStart command
type TaskStart struct {
	*command
	opts   *TaskStartOpts
	parser *flags.Parser
}

//TaskStartFactory returns a factory method for the join command
func TaskStartFactory() (cli.Command, error) {
	cmd := &TaskStart{
		command: &command{
			help:     "",
			synopsis: "schedule a new task for workers to consume from a queue",
			parser:   flags.NewNamedParser("nerd task start <queue-id> <payload>", flags.Default),
			ui: &cli.BasicUi{
				Reader: os.Stdin,
				Writer: os.Stderr,
			},
		},

		opts: &TaskStartOpts{},
	}

	cmd.runFunc = cmd.DoRun
	_, err := cmd.command.parser.AddGroup("options", "options", cmd.opts)
	if err != nil {
		panic(err)
	}

	return cmd, nil
}

//DoRun is called by run and allows an error to be returned
func (cmd *TaskStart) DoRun(args []string) (err error) {
	if len(args) < 2 {
		return fmt.Errorf("not enough arguments, see --help")
	}

	config, err := conf.Read()
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}

	bclient, err := NewClient(cmd.ui)
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}

	v := map[string]interface{}{}
	err = json.Unmarshal([]byte(args[1]), &v)
	if err != nil {
		HandleError(errors.Errorf("payload must a valid JSON map, received: '%s'", args[1]), cmd.opts.VerboseOutput)
	}

	//@TODO get cmd, map and stdin from args
	out, err := bclient.StartTask(config.CurrentProject, args[0], []string{}, map[string]string{}, []byte{})
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}

	logrus.Infof("Task Start: %v", out)
	return nil
}<commit_msg>task start now takes env and stdin<commit_after>package command

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"strings"

	"github.com/Sirupsen/logrus"
	"github.com/jessevdk/go-flags"
	"github.com/mattn/go-isatty"
	"github.com/mitchellh/cli"
	"github.com/nerdalize/nerd/nerd/conf"
)

//TaskStartOpts describes command options
type TaskStartOpts struct {
	NerdOpts
	Env []string `long:"env" short:"e" description:"environment variables to"`
}

//TaskStart command
type TaskStart struct {
	*command
	opts   *TaskStartOpts
	parser *flags.Parser
}

//TaskStartFactory returns a factory method for the join command
func TaskStartFactory() (cli.Command, error) {
	cmd := &TaskStart{
		command: &command{
			help:     "",
			synopsis: "schedule a new task for workers to consume from a queue",
			parser:   flags.NewNamedParser("nerd task start <queue-id> [<cmd_arg1>, <cmd_arg2>]", flags.Default),
			ui: &cli.BasicUi{
				Reader: os.Stdin,
				Writer: os.Stderr,
			},
		},

		opts: &TaskStartOpts{},
	}

	cmd.runFunc = cmd.DoRun
	_, err := cmd.command.parser.AddGroup("options", "options", cmd.opts)
	if err != nil {
		panic(err)
	}

	return cmd, nil
}

//DoRun is called by run and allows an error to be returned
func (cmd *TaskStart) DoRun(args []string) (err error) {
	if len(args) < 1 {
		return fmt.Errorf("not enough arguments, see --help")
	}

	config, err := conf.Read()
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}

	bclient, err := NewClient(cmd.ui)
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}

	tcmd := []string{}
	if len(args) > 1 {
		tcmd = args[1:]
	}

	tenv := map[string]string{}
	for _, l := range cmd.opts.Env {
		split := strings.SplitN(l, "=", 2)
		if len(split) < 2 {
			HandleError(fmt.Errorf("invalid environment variable format, expected 'FOO=bar' fromat, got: %v", l), cmd.opts.VerboseOutput)
		}
		tenv[split[0]] = split[1]
	}

	buf := bytes.NewBuffer(nil)
	if !isatty.IsTerminal(os.Stdin.Fd()) {
		lr := io.LimitReader(os.Stdin, 128*1024) //128KiB
		_, err = io.Copy(buf, lr)
		if err != nil {
			HandleError(fmt.Errorf("failed to copy stdin: %v", err), cmd.opts.VerboseOutput)
		}
	}

	out, err := bclient.StartTask(config.CurrentProject, args[0], tcmd, tenv, buf.Bytes())
	if err != nil {
		HandleError(err, cmd.opts.VerboseOutput)
	}

	logrus.Infof("Task Start: %v", out)
	return nil
}
{'fraction_non_alphanumeric': 0.152659069325736, 'fraction_numerical': 0.005698005698005698, 'mean_word_length': 3.8988372093023256, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '5071147', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1560, 'n_words': 393}