<commit_before>// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"bytes"
	"errors"
	"flag"
	"fmt"
	"io"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
)

// env holds a small amount of Go environment and toolchain information
// which is common to multiple builders. Most Bazel-agnostic build information
// is collected in go/build.Default though.
//
// See ./README.rst for more information about handling arguments and
// environment variables.
type env struct {
	// go_ is the path to the go executable
	go_ string

	// verbose indicates whether subprocess command lines should be printed.
	verbose bool
}

// envFlags registers flags common to multiple builders and returns an env
// configured with those flags.
func envFlags(flags *flag.FlagSet) *env {
	env := &env{}
	flags.StringVar(&env.go_, "go", "", "The path to the go tool.")
	flags.Var(&tagFlag{}, "tags", "List of build tags considered true.")
	flags.BoolVar(&env.verbose, "v", false, "Whether subprocess command lines should be printed")
	return env
}

// checkFlags checks whether env flags were set to valid values. checkFlags
// should be called after parsing flags.
func (e *env) checkFlags() error {
	if e.go_ == "" {
		return errors.New("-go was not specified")
	}
	return nil
}

// runGoCommand executes a subprocess through the go tool. The subprocess will
// inherit stdout, stderr, and the environment from this process.
func (e *env) runGoCommand(goargs []string) error {
	cmd := exec.Command(e.go_, goargs...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return runAndLogCommand(cmd, e.verbose)
}

// runGoCommandToFile executes a subprocess through the go tool and writes
// the output to the given writer.
func (e *env) runGoCommandToFile(w io.Writer, goargs []string) error {
	cmd := exec.Command(e.go_, goargs...)
	cmd.Stdout = w
	cmd.Stderr = os.Stderr
	return runAndLogCommand(cmd, e.verbose)
}

func runAndLogCommand(cmd *exec.Cmd, verbose bool) error {
	if err := cmd.Run(); err != nil {
		var buf bytes.Buffer
		formatCommand(&buf, cmd)
		return fmt.Errorf("error running subcommand:\n%s\n%v", buf.String(), err)
	}
	if verbose {
		formatCommand(os.Stderr, cmd)
	}
	return nil
}<commit_msg>Don't print subcommands with error messages (#1484)<commit_after>// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"errors"
	"flag"
	"fmt"
	"io"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
)

// env holds a small amount of Go environment and toolchain information
// which is common to multiple builders. Most Bazel-agnostic build information
// is collected in go/build.Default though.
//
// See ./README.rst for more information about handling arguments and
// environment variables.
type env struct {
	// go_ is the path to the go executable
	go_ string

	// verbose indicates whether subprocess command lines should be printed.
	verbose bool
}

// envFlags registers flags common to multiple builders and returns an env
// configured with those flags.
func envFlags(flags *flag.FlagSet) *env {
	env := &env{}
	flags.StringVar(&env.go_, "go", "", "The path to the go tool.")
	flags.Var(&tagFlag{}, "tags", "List of build tags considered true.")
	flags.BoolVar(&env.verbose, "v", false, "Whether subprocess command lines should be printed")
	return env
}

// checkFlags checks whether env flags were set to valid values. checkFlags
// should be called after parsing flags.
func (e *env) checkFlags() error {
	if e.go_ == "" {
		return errors.New("-go was not specified")
	}
	return nil
}

// runGoCommand executes a subprocess through the go tool. The subprocess will
// inherit stdout, stderr, and the environment from this process.
func (e *env) runGoCommand(goargs []string) error {
	cmd := exec.Command(e.go_, goargs...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return runAndLogCommand(cmd, e.verbose)
}

// runGoCommandToFile executes a subprocess through the go tool and writes
// the output to the given writer.
func (e *env) runGoCommandToFile(w io.Writer, goargs []string) error {
	cmd := exec.Command(e.go_, goargs...)
	cmd.Stdout = w
	cmd.Stderr = os.Stderr
	return runAndLogCommand(cmd, e.verbose)
}

func runAndLogCommand(cmd *exec.Cmd, verbose bool) error {
	if verbose {
		formatCommand(os.Stderr, cmd)
	}
	if err := cmd.Run(); err != nil {
		return fmt.Errorf("error running subcommand: %v", err)
	}
	return nil
}
{'fraction_non_alphanumeric': 0.10865628395508874, 'fraction_numerical': 0.003621876131836291, 'mean_word_length': 3.761206896551724, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6578519', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1633, 'n_words': 678}