<commit_before>package main

import (
	"flag"
	"fmt"
	"io"
	"os"
	"path"
	"strings"

	"github.com/vito/gocart/command_runner"
	"github.com/vito/gocart/dependency"
	"github.com/vito/gocart/dependency_fetcher"
	"github.com/vito/gocart/dependency_reader"
	"github.com/vito/gocart/gopath"
	"github.com/vito/gocart/locker"
)

const GocartVersion = "0.1.0"

const CartridgeFile = "Cartridge"
const CartridgeLockFile = "Cartridge.lock"

var GOPATH string

var FetchedDependencies = make(map[string]dependency.Dependency)
var TrickleDownDependencies = make(map[string]dependency.Dependency)

var recursive = flag.Bool(
	"r",
	false,
	"recursively fetch dependencies and check for conflicts",
)

var showHelp = flag.Bool(
	"h",
	false,
	"show command usage (this text)",
)

var showVersion = flag.Bool(
	"v",
	false,
	"show gocart version",
)

var trickleDown = flag.Bool(
	"t",
	false,
	"trickle down dependencies into nested Cartridges",
)

var aggregate = flag.Bool(
	"a",
	false,
	"collect recursive dependencies into a single .lock file",
)

func main() {
	flag.Parse()

	args := flag.Args()

	command := ""

	gopath, err := gopath.InstallationDirectory(os.Getenv("GOPATH"))
	if err != nil {
		fatal("GOPATH is not set.")
	}

	GOPATH = gopath

	if len(args) == 0 {
		command = "install"
	} else {
		command = args[0]
	}

	if *showHelp || command == "help" {
		help()
		return
	}

	if *showVersion || command == "version" {
		version()
		return
	}

	if command == "install" {
		install(".", *recursive, *aggregate, *trickleDown, 0)
		return
	}

	unknownCommand()
}

func install(root string, recursive bool, aggregate bool, trickleDown bool, depth int) {
	if _, err := os.Stat(path.Join(root, CartridgeFile)); err != nil {
		println("no Cartridge file!")
		os.Exit(1)
		return
	}

	requestedDependencies := loadFile(path.Join(root, CartridgeFile))
	lockedDependencies := loadFile(path.Join(root, CartridgeLockFile))

	dependencies := locker.GenerateLock(requestedDependencies, lockedDependencies)

	newLockedDependencies := installDependencies(dependencies, recursive, trickleDown, depth)

	file, err := os.Create(path.Join(root, "Cartridge.lock"))
	if err != nil {
		fatal(err.Error())
	}
	defer file.Close()

	var dependenciesToBeWritten []dependency.Dependency

	if aggregate {
		for _, dep := range FetchedDependencies {
			dependenciesToBeWritten = append(dependenciesToBeWritten, dep)
		}
	} else {
		dependenciesToBeWritten = newLockedDependencies
	}

	err = updateLockFile(file, dependenciesToBeWritten)
	if err != nil {
		fatal(err.Error())
	}

	if depth == 0 {
		fmt.Println("\x1b[32mOK\x1b[0m")
	}
}

func help() {
	fmt.Println(`gocart: a go package manager

Usage:

    'gocart install' or 'gocart':
        Install dependencies described by Cartridge.lock or Cartridge, and
        update Cartridge.lock with locked-down dependency versions.

Place your dependencies in a file called Cartridge with this format:
<commit_msg>update usage text to mention -r, -a, -t<commit_after>package main

import (
	"flag"
	"fmt"
	"io"
	"os"
	"path"
	"strings"

	"github.com/vito/gocart/command_runner"
	"github.com/vito/gocart/dependency"
	"github.com/vito/gocart/dependency_fetcher"
	"github.com/vito/gocart/dependency_reader"
	"github.com/vito/gocart/gopath"
	"github.com/vito/gocart/locker"
)

const GocartVersion = "0.1.0"

const CartridgeFile = "Cartridge"
const CartridgeLockFile = "Cartridge.lock"

var GOPATH string

var FetchedDependencies = make(map[string]dependency.Dependency)
var TrickleDownDependencies = make(map[string]dependency.Dependency)

var recursive = flag.Bool(
	"r",
	false,
	"recursively fetch dependencies and check for conflicts",
)

var showHelp = flag.Bool(
	"h",
	false,
	"show command usage (this text)",
)

var showVersion = flag.Bool(
	"v",
	false,
	"show gocart version",
)

var trickleDown = flag.Bool(
	"t",
	false,
	"trickle down dependencies into nested Cartridges",
)

var aggregate = flag.Bool(
	"a",
	false,
	"collect recursive dependencies into a single .lock file",
)

func main() {
	flag.Parse()

	args := flag.Args()

	command := ""

	gopath, err := gopath.InstallationDirectory(os.Getenv("GOPATH"))
	if err != nil {
		fatal("GOPATH is not set.")
	}

	GOPATH = gopath

	if len(args) == 0 {
		command = "install"
	} else {
		command = args[0]
	}

	if *showHelp || command == "help" {
		help()
		return
	}

	if *showVersion || command == "version" {
		version()
		return
	}

	if command == "install" {
		install(".", *recursive, *aggregate, *trickleDown, 0)
		return
	}

	unknownCommand()
}

func install(root string, recursive bool, aggregate bool, trickleDown bool, depth int) {
	if _, err := os.Stat(path.Join(root, CartridgeFile)); err != nil {
		println("no Cartridge file!")
		os.Exit(1)
		return
	}

	requestedDependencies := loadFile(path.Join(root, CartridgeFile))
	lockedDependencies := loadFile(path.Join(root, CartridgeLockFile))

	dependencies := locker.GenerateLock(requestedDependencies, lockedDependencies)

	newLockedDependencies := installDependencies(dependencies, recursive, trickleDown, depth)

	file, err := os.Create(path.Join(root, "Cartridge.lock"))
	if err != nil {
		fatal(err.Error())
	}
	defer file.Close()

	var dependenciesToBeWritten []dependency.Dependency

	if aggregate {
		for _, dep := range FetchedDependencies {
			dependenciesToBeWritten = append(dependenciesToBeWritten, dep)
		}
	} else {
		dependenciesToBeWritten = newLockedDependencies
	}

	err = updateLockFile(file, dependenciesToBeWritten)
	if err != nil {
		fatal(err.Error())
	}

	if depth == 0 {
		fmt.Println("\x1b[32mOK\x1b[0m")
	}
}

func help() {
	fmt.Println(`gocart: a go package manager

Usage:

  'gocart':
    Install dependencies described by Cartridge.lock or Cartridge, and
    update Cartridge.lock with locked-down dependency versions.

    The following flags are handled:

      -r: (recurse) if each dependency has a Cartridge, recursively run
          gocart for it as well

      -t: (trickle down) with -r, override lower-level dependencies with
          root dependencies

      -a: (aggregate) with -r, collect all recursive dependencies into the
          top level lockfile

Place your dependencies in a file called Cartridge with this format:

{'fraction_non_alphanumeric': 0.12431561996779388, 'fraction_numerical': 0.004186795491143317, 'mean_word_length': 4.236930860033727, 'pattern_counts': {'":': 0, '<': 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': '3148257', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2095, 'n_words': 536}