<commit_before>package tachyon

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

var cUpdateScript = []byte(`#!/bin/bash

cd .tachyon

REL=$TACHYON_RELEASE
BIN=tachyon-$TACHYON_OS-$TACHYON_ARCH

if test -f tachyon; then
  CUR=$(< release)
  if test "$REL" != "$CUR"; then
    echo "Detected tachyon of old release ($CUR), removing."
    rm tachyon
  fi
fi

if which curl > /dev/null; then
  DL="curl -O"
elif which wget > /dev/null; then
  DL="wget"
else
  echo "No curl or wget, unable to pull a release"
  exit 1
fi

if ! test -f tachyon; then
  echo "Downloading $REL/$BIN..."

  $DL https://s3-us-west-2.amazonaws.com/tachyon.vektra.io/$REL/sums
  if which gpg > /dev/null; then
    gpg --keyserver keys.gnupg.net --recv-key A408199F &
    $DL https://s3-us-west-2.amazonaws.com/tachyon.vektra.io/$REL/sums.asc &
  fi

  $DL https://s3-us-west-2.amazonaws.com/tachyon.vektra.io/$REL/$BIN

  wait

  if which gpg > /dev/null; then
    if ! gpg --verify sums.asc; then
      echo "Signature verification failed! Aborting!"
      exit 1
    fi
  fi

  mv $BIN $BIN.gz

  # If gunzip fails, it's because the file isn't gzip'd, so we
  # assume it's already in the correct format.
  if ! gunzip $BIN.gz; then
    mv $BIN.gz $BIN
  fi

  if which shasum > /dev/null; then
    if ! (grep $BIN sums | shasum -c); then
      echo "Sum verification failed!"
      exit 1
    fi
  else
    echo "No shasum available to verify files"
  fi

  echo $REL > release

  chmod a+x $BIN
  ln -s $BIN tachyon
fi
`)

func normalizeArch(arch string) string {
	switch arch {
	case "x86_64":
		return "amd64"
	default:
		return arch
	}
}

type Tachyon struct {
	Target   string `tachyon:"target"`
	Debug    bool   `tachyon:"debug"`
	Clean    bool   `tachyon:"clean"`
	Dev      bool   `tachyon:"dev"`
	Playbook string `tachyon:"playbook"`
	Release  string `tachyon:"release"`
	NoJSON   bool   `tachyon:"no_json"`
}

func (t *Tachyon) Run(env *CommandEnv) (*Result, error) {
	if t.Release == "" {
		t.Release = Release
	}

	ssh := NewSSH(t.Target)
	ssh.Debug = t.Debug

	defer ssh.Cleanup()

	// err := ssh.Start()
	// if err != nil {
	// return nil, fmt.Errorf("Error starting persistent SSH connection: %s\n", err)
	// }

	var bootstrap string

	if t.Clean {
		bootstrap = "rm -rf .tachyon && mkdir -p .tachyon"
	} else {
		bootstrap = "mkdir -p .tachyon"
	}

	out, err := ssh.RunAndCapture(bootstrap + " && uname && uname -m")
	if err != nil {
		return nil, fmt.Errorf("Error creating remote .tachyon dir: %s (%s)", err, string(out))
	}

	tos, arch, ok := split2(string(out), "\n")
	if !ok {
		return nil, fmt.Errorf("Unable to figure out os and arch of remote machine\n")
	}

	tos = strings.ToLower(tos)
	arch = normalizeArch(strings.TrimSpace(arch))

	binary := fmt.Sprintf("tachyon-%s-%s", tos, arch)

	if t.Dev {
		env.Progress("Copying development tachyon...")

		path := filepath.Dir(Arg0)

		err = ssh.CopyToHost(filepath.Join(path, binary), ".tachyon/"+binary+".new")
		if err != nil {
			return nil, fmt.Errorf("Error copying tachyon to vagrant: %s\n", err)
		}

		ssh.Run(fmt.Sprintf("cd .tachyon && mv %[1]s.new %[1]s && ln -fs %[1]s tachyon", binary))
	} else {
		env.Progress("Updating tachyon release...")

		c := ssh.Command("cat > .tachyon/update && chmod a+x .tachyon/update")

		c.Stdout = os.Stdout
		c.Stdin = bytes.NewReader(cUpdateScript)
		err = c.Run()
		if err != nil {
			return nil, fmt.Errorf("Error updating, well, the updater: %s\n", err)
		}

		cmd := fmt.Sprintf("TACHYON_RELEASE=%s TACHYON_OS=%s TACHYON_ARCH=%s ./.tachyon/update", t.Release, tos, arch)
		err = ssh.Run(cmd)
		if err != nil {
			return nil, fmt.Errorf("Error running updater: %s\n", err)
		}
	}

	var src string

	var main string

	fi, err := os.Stat(t.Playbook)
	if fi.IsDir() {
		src, err = filepath.Abs(t.Playbook)
		if err != nil {
			return nil, fmt.Errorf("Unable to resolve %s: %s", t.Playbook, err)
		}
		main = "site.yml"
	} else {
		abs, err := filepath.Abs(t.Playbook)
		if err != nil {
			return nil, fmt.Errorf("Unable to resolve %s: %s", t.Playbook, err)
		}

		main = filepath.Base(abs)
		src = filepath.Dir(abs)
	}

	src += "/"<commit_msg>Gracefully handle refering to an invalid playbook<commit_after>package tachyon

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

var cUpdateScript = []byte(`#!/bin/bash

cd .tachyon

REL=$TACHYON_RELEASE
BIN=tachyon-$TACHYON_OS-$TACHYON_ARCH

if test -f tachyon; then
  CUR=$(< release)
  if test "$REL" != "$CUR"; then
    echo "Detected tachyon of old release ($CUR), removing."
    rm tachyon
  fi
fi

if which curl > /dev/null; then
  DL="curl -O"
elif which wget > /dev/null; then
  DL="wget"
else
  echo "No curl or wget, unable to pull a release"
  exit 1
fi

if ! test -f tachyon; then
  echo "Downloading $REL/$BIN..."

  $DL https://s3-us-west-2.amazonaws.com/tachyon.vektra.io/$REL/sums
  if which gpg > /dev/null; then
    gpg --keyserver keys.gnupg.net --recv-key A408199F &
    $DL https://s3-us-west-2.amazonaws.com/tachyon.vektra.io/$REL/sums.asc &
  fi

  $DL https://s3-us-west-2.amazonaws.com/tachyon.vektra.io/$REL/$BIN

  wait

  if which gpg > /dev/null; then
    if ! gpg --verify sums.asc; then
      echo "Signature verification failed! Aborting!"
      exit 1
    fi
  fi

  mv $BIN $BIN.gz

  # If gunzip fails, it's because the file isn't gzip'd, so we
  # assume it's already in the correct format.
  if ! gunzip $BIN.gz; then
    mv $BIN.gz $BIN
  fi

  if which shasum > /dev/null; then
    if ! (grep $BIN sums | shasum -c); then
      echo "Sum verification failed!"
      exit 1
    fi
  else
    echo "No shasum available to verify files"
  fi

  echo $REL > release

  chmod a+x $BIN
  ln -s $BIN tachyon
fi
`)

func normalizeArch(arch string) string {
	switch arch {
	case "x86_64":
		return "amd64"
	default:
		return arch
	}
}

type Tachyon struct {
	Target   string `tachyon:"target"`
	Debug    bool   `tachyon:"debug"`
	Clean    bool   `tachyon:"clean"`
	Dev      bool   `tachyon:"dev"`
	Playbook string `tachyon:"playbook"`
	Release  string `tachyon:"release"`
	NoJSON   bool   `tachyon:"no_json"`
}

func (t *Tachyon) Run(env *CommandEnv) (*Result, error) {
	if t.Release == "" {
		t.Release = Release
	}

	ssh := NewSSH(t.Target)
	ssh.Debug = t.Debug

	defer ssh.Cleanup()

	// err := ssh.Start()
	// if err != nil {
	// return nil, fmt.Errorf("Error starting persistent SSH connection: %s\n", err)
	// }

	var bootstrap string

	if t.Clean {
		bootstrap = "rm -rf .tachyon && mkdir -p .tachyon"
	} else {
		bootstrap = "mkdir -p .tachyon"
	}

	out, err := ssh.RunAndCapture(bootstrap + " && uname && uname -m")
	if err != nil {
		return nil, fmt.Errorf("Error creating remote .tachyon dir: %s (%s)", err, string(out))
	}

	tos, arch, ok := split2(string(out), "\n")
	if !ok {
		return nil, fmt.Errorf("Unable to figure out os and arch of remote machine\n")
	}

	tos = strings.ToLower(tos)
	arch = normalizeArch(strings.TrimSpace(arch))

	binary := fmt.Sprintf("tachyon-%s-%s", tos, arch)

	if t.Dev {
		env.Progress("Copying development tachyon...")

		path := filepath.Dir(Arg0)

		err = ssh.CopyToHost(filepath.Join(path, binary), ".tachyon/"+binary+".new")
		if err != nil {
			return nil, fmt.Errorf("Error copying tachyon to vagrant: %s\n", err)
		}

		ssh.Run(fmt.Sprintf("cd .tachyon && mv %[1]s.new %[1]s && ln -fs %[1]s tachyon", binary))
	} else {
		env.Progress("Updating tachyon release...")

		c := ssh.Command("cat > .tachyon/update && chmod a+x .tachyon/update")

		c.Stdout = os.Stdout
		c.Stdin = bytes.NewReader(cUpdateScript)
		err = c.Run()
		if err != nil {
			return nil, fmt.Errorf("Error updating, well, the updater: %s\n", err)
		}

		cmd := fmt.Sprintf("TACHYON_RELEASE=%s TACHYON_OS=%s TACHYON_ARCH=%s ./.tachyon/update", t.Release, tos, arch)
		err = ssh.Run(cmd)
		if err != nil {
			return nil, fmt.Errorf("Error running updater: %s\n", err)
		}
	}

	var src string

	var main string

	fi, err := os.Stat(t.Playbook)
	if err != nil {
		return nil, err
	}

	if fi.IsDir() {
		src, err = filepath.Abs(t.Playbook)
		if err != nil {
			return nil, fmt.Errorf("Unable to resolve %s: %s", t.Playbook, err)
		}
		main = "site.yml"
	} else {
		abs, err := filepath.Abs(t.Playbook)
		if err != nil {
			return nil, fmt.Errorf("Unable to resolve %s: %s", t.Playbook, err)
		}

		main = filepath.Base(abs)
		src = filepath.Dir(abs)
	}

	src += "/"
{'fraction_non_alphanumeric': 0.15591334201491655, 'fraction_numerical': 0.006156031727240441, 'mean_word_length': 3.0401721664275465, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7673966', 'n_tokens_mistral': 3589, 'n_tokens_neox': 3294, 'n_words': 902}