<commit_before>package main

import (
	"archive/tar"
	"bytes"
	"compress/gzip"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/appc/spec/aci"
	"github.com/appc/spec/schema"
	"github.com/appc/spec/schema/types"
)

var Debug bool

func warn(s string, i ...interface{}) {
	s = fmt.Sprintf(s, i...)
	fmt.Fprintln(os.Stderr, strings.TrimSuffix(s, "\n"))
}

func die(s string, i ...interface{}) {
	warn(s, i...)
	os.Exit(1)
}

func debug(i ...interface{}) {
	if Debug {
		s := fmt.Sprint(i...)
		fmt.Fprintln(os.Stderr, strings.TrimSuffix(s, "\n"))
	}
}

type StringVector []string

func (v *StringVector) String() string {
	return `"` + strings.Join(*v, `" "`) + `"`
}

func (v *StringVector) Set(str string) error {
	*v = append(*v, str)
	return nil
}

func main() {
	var (
		execOpts            StringVector
		goDefaultBinaryDesc string
		goBinaryOpt         string
		goPathOpt           string
	)

	// Find the go binary
	gocmd, err := exec.LookPath("go")

	flag.Var(&execOpts, "exec", "Parameters passed to app, can be used multiple times")
	if err != nil {
		goDefaultBinaryDesc = "Go binary to use (default: none found in $PATH, so it must be provided)"
	} else {
		goDefaultBinaryDesc = "Go binary to use (default: whatever go in $PATH)"
	}
	flag.StringVar(&goBinaryOpt, "go-binary", gocmd, goDefaultBinaryDesc)
	flag.StringVar(&goPathOpt, "go-path", "", "Custom GOPATH (default: a temporary directory)")
	flag.Parse()
	if os.Getenv("GOPATH") != "" {
		warn("GOPATH envvar is ignored, use --go-path=\"$GOPATH\" option instead")
	}
	if os.Getenv("GOROOT") != "" {
		warn("GOROOT envvar is ignored, use --go-binary=\"$GOROOT/bin/go\" option instead")
	}
	if os.Getenv("GOACI_DEBUG") != "" {
		Debug = true
	}

	if goBinaryOpt == "" {
		die("go binary not found")
	}

	// Set up a temporary directory for everything (gopath and builds)
	tmpdir, err := ioutil.TempDir("", "goaci")
	if err != nil {
		die("error setting up temporary directory: %v", err)
	}
	defer os.RemoveAll(tmpdir)
	if goPathOpt == "" {
		goPathOpt = tmpdir
	}

	// Scratch build dir for aci
	acidir := filepath.Join(tmpdir, "aci")

	// Let's put final binary in tmpdir
	gobin := filepath.Join(tmpdir, "bin")

	// Construct args for a go get that does a static build
	args := []string{
		goBinaryOpt,
		"get",
		"-a",
		"-tags", "netgo",
		"-ldflags", "'-w'",
		// 1.4
		"-installsuffix", "cgo",
	}

	// Extract the package name (which is the last arg).
	var ns string
	for _, arg := range os.Args[1:] {
		// TODO(jonboulle): try to pass the other args on to go get?
		//		args = append(args, arg)
		ns = arg
	}

	name, err := types.NewACName(ns)
	// TODO(jonboulle): could this ever actually happen?
	if err != nil {
		die("bad app name: %v", err)
	}
	args = append(args, ns)

	// Use the last component, e.g. example.com/my/app --> app
	ofn := filepath.Base(ns) + ".aci"
	mode := os.O_CREATE | os.O_WRONLY | os.O_TRUNC
	of, err := os.OpenFile(ofn, mode, 0644)
	if err != nil {
		die("error opening output file: %v", err)
	}

	cmd := exec.Cmd{
		Env: []string{
			"GOPATH=" + goPathOpt,
			"GOBIN=" + gobin,
			"CGO_ENABLED=0",
			"PATH=" + os.Getenv("PATH"),
		},
		Path:   goBinaryOpt,
		Args:   args,
		Stderr: os.Stderr,
		Stdout: os.Stdout,
	}
	debug("env:", cmd.Env)
	debug("running command:", strings.Join(cmd.Args, " "))
	if err := cmd.Run(); err != nil {
		die("error running go: %v", err)
	}

	// Check that we got 1 binary from the go get command
	fi, err := ioutil.ReadDir(gobin)<commit_msg>Forward GOROOT env var to go get<commit_after>package main

import (
	"archive/tar"
	"bytes"
	"compress/gzip"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/appc/spec/aci"
	"github.com/appc/spec/schema"
	"github.com/appc/spec/schema/types"
)

var Debug bool

func warn(s string, i ...interface{}) {
	s = fmt.Sprintf(s, i...)
	fmt.Fprintln(os.Stderr, strings.TrimSuffix(s, "\n"))
}

func die(s string, i ...interface{}) {
	warn(s, i...)
	os.Exit(1)
}

func debug(i ...interface{}) {
	if Debug {
		s := fmt.Sprint(i...)
		fmt.Fprintln(os.Stderr, strings.TrimSuffix(s, "\n"))
	}
}

type StringVector []string

func (v *StringVector) String() string {
	return `"` + strings.Join(*v, `" "`) + `"`
}

func (v *StringVector) Set(str string) error {
	*v = append(*v, str)
	return nil
}

func main() {
	var (
		execOpts            StringVector
		goDefaultBinaryDesc string
		goBinaryOpt         string
		goPathOpt           string
	)

	// Find the go binary
	gocmd, err := exec.LookPath("go")

	flag.Var(&execOpts, "exec", "Parameters passed to app, can be used multiple times")
	if err != nil {
		goDefaultBinaryDesc = "Go binary to use (default: none found in $PATH, so it must be provided)"
	} else {
		goDefaultBinaryDesc = "Go binary to use (default: whatever go in $PATH)"
	}
	flag.StringVar(&goBinaryOpt, "go-binary", gocmd, goDefaultBinaryDesc)
	flag.StringVar(&goPathOpt, "go-path", "", "Custom GOPATH (default: a temporary directory)")
	flag.Parse()
	if os.Getenv("GOPATH") != "" {
		warn("GOPATH env var is ignored, use --go-path=\"$GOPATH\" option instead")
	}
	goRoot := os.Getenv("GOROOT")
	if goRoot != "" {
		warn("Overriding GOROOT env var to %s", goRoot)
	}
	if os.Getenv("GOACI_DEBUG") != "" {
		Debug = true
	}

	if goBinaryOpt == "" {
		die("go binary not found")
	}

	// Set up a temporary directory for everything (gopath and builds)
	tmpdir, err := ioutil.TempDir("", "goaci")
	if err != nil {
		die("error setting up temporary directory: %v", err)
	}
	defer os.RemoveAll(tmpdir)
	if goPathOpt == "" {
		goPathOpt = tmpdir
	}

	// Scratch build dir for aci
	acidir := filepath.Join(tmpdir, "aci")

	// Let's put final binary in tmpdir
	gobin := filepath.Join(tmpdir, "bin")

	// Construct args for a go get that does a static build
	args := []string{
		goBinaryOpt,
		"get",
		"-a",
		"-tags", "netgo",
		"-ldflags", "'-w'",
		// 1.4
		"-installsuffix", "cgo",
	}

	// Extract the package name (which is the last arg).
	var ns string
	for _, arg := range os.Args[1:] {
		// TODO(jonboulle): try to pass the other args on to go get?
		//		args = append(args, arg)
		ns = arg
	}

	name, err := types.NewACName(ns)
	// TODO(jonboulle): could this ever actually happen?
	if err != nil {
		die("bad app name: %v", err)
	}
	args = append(args, ns)

	// Use the last component, e.g. example.com/my/app --> app
	ofn := filepath.Base(ns) + ".aci"
	mode := os.O_CREATE | os.O_WRONLY | os.O_TRUNC
	of, err := os.OpenFile(ofn, mode, 0644)
	if err != nil {
		die("error opening output file: %v", err)
	}

	env := []string{
		"GOPATH=" + goPathOpt,
		"GOBIN=" + gobin,
		"CGO_ENABLED=0",
		"PATH=" + os.Getenv("PATH"),
	}
	if goRoot != "" {
		env = append(env, "GOROOT="+goRoot)
	}
	cmd := exec.Cmd{
		Env:    env,
		Path:   goBinaryOpt,
		Args:   args,
		Stderr: os.Stderr,
		Stdout: os.Stdout,
	}
	debug("env:", cmd.Env)
	debug("running command:", strings.Join(cmd.Args, " "))
	if err := cmd.Run(); err != nil {
		die("error running go: %v", err)
	}

	// Check that we got 1 binary from the go get command
	fi, err := ioutil.ReadDir(gobin)
{'fraction_non_alphanumeric': 0.17287084038353073, 'fraction_numerical': 0.0028200789622109417, 'mean_word_length': 3.278045838359469, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4948108', 'n_tokens_mistral': 2864, 'n_tokens_neox': 2735, 'n_words': 750}